Skip to content

Commit 4cb665f

Browse files
committed
valid-anagram 풀이 추가
1 parent 8a79333 commit 4cb665f

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

valid-anagram/junzero741.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// TC: O(n)
2+
// SC: O(n)
3+
function isAnagram(s: string, t: string): boolean {
4+
if(s.length !== t.length) {
5+
return false;
6+
}
7+
8+
const sMap = new Map<string, number>();
9+
const tMap = new Map<string, number>();
10+
11+
for(let i = 0; i < s.length; i++) { // TC: O(N), SC: O(N)
12+
const sChar = s[i];
13+
const tChar = t[i];
14+
const sCharCount = sMap.get(sChar) || 0;
15+
const tCharCount = tMap.get(tChar) || 0;
16+
17+
sMap.set(sChar, sCharCount + 1);
18+
tMap.set(tChar, tCharCount + 1);
19+
}
20+
21+
for(let i = 0; i < s.length; i++) { // TC: O(N), SC: O(1)
22+
const char = s[i];
23+
24+
if(sMap.get(char) !== tMap.get(char)) {
25+
return false;
26+
}
27+
}
28+
29+
return true;
30+
};

0 commit comments

Comments
 (0)