We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ac59579 commit 4d68b2aCopy full SHA for 4d68b2a
valid-anagram/HC-kang.ts
@@ -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