Skip to content

Commit be4fb1c

Browse files
committed
valid anagram solution
1 parent 34ab358 commit be4fb1c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

valid-anagram/hyer0705.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function isAnagram(s: string, t: string): boolean {
2+
if (s.length !== t.length) return false;
3+
4+
const sCountMap = new Map<string, number>();
5+
const tCountMap = new Map<string, number>();
6+
7+
for (let i = 0; i < s.length; i++) {
8+
const currentS = s[i];
9+
const currentT = t[i];
10+
11+
sCountMap.set(currentS, (sCountMap.get(currentS) || 0) + 1);
12+
tCountMap.set(currentT, (tCountMap.get(currentT) || 0) + 1);
13+
}
14+
15+
for (const [k, v] of sCountMap) {
16+
if (!tCountMap.has(k) || tCountMap.get(k) !== v) return false;
17+
}
18+
19+
return true;
20+
}

0 commit comments

Comments
 (0)