Skip to content

Commit be0e435

Browse files
committed
add valid-anagram solution
1 parent 88e8975 commit be0e435

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

valid-anagram/nakjun12.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function isAnagram(s: string, t: string): boolean {
2+
if (s.length !== t.length) return false;
3+
4+
// 공간 복잡도: O(k)
5+
const hash: { [key: string]: number } = {};
6+
7+
// 시간 복잡도: O(n)
8+
for (let char of s) {
9+
hash[char] = (hash[char] || 0) + 1;
10+
}
11+
12+
// 시간 복잡도: O(n)
13+
for (let char of t) {
14+
if (!hash[char]) return false;
15+
hash[char]--;
16+
}
17+
18+
return true;
19+
}

0 commit comments

Comments
 (0)