Skip to content

Commit e6007ae

Browse files
committed
solve: valid anagram
1 parent 2397584 commit e6007ae

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

valid-anagram/tolluset.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(n)
4+
* */
5+
function isAnagram(s: string, t: string): boolean {
6+
if (s.length !== t.length) {
7+
return false;
8+
}
9+
10+
const groupS = groupBy(s);
11+
const groupT = groupBy(t);
12+
13+
return Object.keys(groupS).every((k) => groupS[k] === groupT[k]);
14+
}
15+
16+
const groupBy = (v: string) =>
17+
v.split("").reduce((acc, cur) => ((acc[cur] = (acc[cur] ?? 0) + 1), acc), {});

0 commit comments

Comments
 (0)