Skip to content

Commit 2790d3d

Browse files
authored
Create 2068-check-whether-two-strings-are-almost-equivalent.js
1 parent a9206f3 commit 2790d3d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} word1
3+
* @param {string} word2
4+
* @return {boolean}
5+
*/
6+
const checkAlmostEquivalent = function(word1, word2) {
7+
const a = 'a'.charCodeAt(0), n = word1.length
8+
const arr1 = Array(26).fill(0), arr2 = Array(26).fill(0)
9+
10+
for(const ch of word1) {
11+
arr1[ch.charCodeAt(0) - a]++
12+
}
13+
for(const ch of word2) {
14+
arr2[ch.charCodeAt(0) - a]++
15+
}
16+
for(let i = 0; i < 26; i++) {
17+
if(Math.abs(arr1[i] - arr2[i]) > 3) return false
18+
}
19+
20+
return true
21+
};

0 commit comments

Comments
 (0)