Skip to content

Commit 10b06b9

Browse files
committed
feat(soobing): valid-anagram
1 parent fdf3120 commit 10b06b9

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

valid-anagram/soobing.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function isAnagram(s: string, t: string): boolean {
2+
const map = new Map();
3+
for (let i = 0; i < s.length; i++) {
4+
map.set(s[i], (map.get(s[i]) || 0) + 1);
5+
}
6+
for (let i = 0; i < t.length; i++) {
7+
if (!map.has(t[i])) {
8+
return false;
9+
}
10+
const newCount = map.get(t[i]) - 1;
11+
if (newCount < 0) return false;
12+
if (newCount === 0) map.delete(t[i]);
13+
else map.set(t[i], map.get(t[i]) - 1);
14+
}
15+
16+
return map.size === 0;
17+
}

0 commit comments

Comments
 (0)