Skip to content

Commit 4d68b2a

Browse files
committed
feat: DaleStudy#242 Valid Anagram
1 parent ac59579 commit 4d68b2a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

valid-anagram/HC-kang.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// T.C: O(n)
2+
// S.C: O(1)
3+
function isAnagram(s: string, t: string): boolean {
4+
if (s.length !== t.length) return false;
5+
6+
const NUM_OF_ALPHA = 26;
7+
const A_CODE = 'a'.charCodeAt(0);
8+
const bucket = new Array(NUM_OF_ALPHA).fill(0); // S.C: O(1)
9+
10+
for (let i = 0; i < s.length; i++) { // T.C: O(n)
11+
bucket[s.charCodeAt(i) - A_CODE]++;
12+
bucket[t.charCodeAt(i) - A_CODE]--;
13+
}
14+
15+
return bucket.every(count => count === 0);
16+
}

0 commit comments

Comments
 (0)