Skip to content

Commit 6bcbeb0

Browse files
authored
Update index.js
1 parent 8cd28db commit 6bcbeb0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

leetcode/valid-anagram/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
var isAnagram = function(s, t) {
7+
if (s.length !== t.length) {
8+
return false
9+
}
10+
11+
let sCharCount = {};
12+
13+
for (let i = 0; i < s.length; i++) {
14+
let sChar = s[i];
15+
sCharCount[sChar] = sCharCount[sChar] + 1 || 1;
16+
}
17+
18+
for (let i = 0; i < t.length; i++) {
19+
const tChar = t[i];
20+
if ( !sCharCount[tChar] ) {
21+
return false
22+
} else {
23+
sCharCount[tChar]--
24+
}
25+
}
26+
27+
return true;
28+
};
29+
30+
// ================================================================================================================================================================
31+
132
/**
233
* @param {string} s
334
* @param {string} t

0 commit comments

Comments
 (0)