Skip to content

Commit a9c1996

Browse files
committed
valid anagram solution
1 parent aae15b9 commit a9c1996

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

valid-anagram/hoyeongkwak.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function isAnagram(s: string, t: string): boolean {
2+
// 시간복잡도 O(nlogn), 공간복잡도 O(n)
3+
// const sSorted = s.split('').sort().join(',')
4+
// const tSorted = t.split('').sort().join(',')
5+
// return sSorted === tSorted
6+
7+
/*
8+
시간복잡도 O(n), 공간복잡도 O(1)
9+
*/
10+
if (s.length != t.length) return false
11+
const count = new Array(26).fill(0)
12+
for (let i = 0; i < s.length; i++) {
13+
count[s.charCodeAt(i) - 97]++
14+
count[t.charCodeAt(i) - 97]--
15+
}
16+
return count.every(c => c === 0)
17+
};

0 commit comments

Comments
 (0)