Skip to content

Commit 7dbd804

Browse files
committed
add solution: valid anagram
1 parent c825bbc commit 7dbd804

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

valid-anagram/yihyun-kim1.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
const isAnagram = (s, t) => {
7+
if (s.length !== t.length) return false;
8+
9+
const map = new Map();
10+
11+
for (let char of s) {
12+
map.set(char, (map.get(char) || 0) + 1);
13+
}
14+
15+
for (let char of t) {
16+
if (!map.has(char)) return false;
17+
map.set(char, map.get(char) - 1);
18+
if (map.get(char) < 0) return false;
19+
}
20+
21+
return true;
22+
};

0 commit comments

Comments
 (0)