Skip to content

Commit 1146e80

Browse files
committed
valid anagram
1 parent ee14026 commit 1146e80

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

valid-anagram/Grit03.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
var isAnagram = function (s, t) {
7+
const sMap = new Map();
8+
const tMap = new Map();
9+
for (const str of [...s]) {
10+
sMap.set(str, (sMap.get(str) ?? 0) + 1);
11+
}
12+
13+
for (const str of [...t]) {
14+
tMap.set(str, (tMap.get(str) ?? 0) + 1);
15+
}
16+
17+
if (sMap.size === tMap.size) {
18+
let answer = true;
19+
for (let [str, count] of sMap) {
20+
if (!tMap.get(str) || tMap.get(str) !== sMap.get(str)) {
21+
answer = false;
22+
}
23+
}
24+
return answer;
25+
}
26+
27+
return false;
28+
};

0 commit comments

Comments
 (0)