Skip to content

Commit 33af494

Browse files
committed
1. valid-anagram
1 parent 81d4e12 commit 33af494

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

valid-anagram/whewchews.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function isAnagram(s: string, t: string): boolean {
2+
// TC: O(1)
3+
if (s.length !== t.length) return false;
4+
5+
// SC: O(N)
6+
const count: { [key: string]: number } = {};
7+
8+
// TC: O(N)
9+
for (let i = 0; i <= s.length - 1; i++) {
10+
const sChar = s[i];
11+
const tChar = t[i];
12+
count[sChar] = (count[sChar] || 0) + 1;
13+
count[tChar] = (count[tChar] || 0) - 1;
14+
}
15+
16+
// TC: O(N)
17+
return Object.values(count).every((v) => v === 0);
18+
}
19+
20+
// TC: O(N)
21+
// SC: O(N)

0 commit comments

Comments
 (0)