Skip to content

Commit dfe8f0f

Browse files
authored
valid-anagram solution
1 parent f639445 commit dfe8f0f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

valid-anagram/solbijae.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(n log n), 공간 복잡도 O(n)
3+
// const sSort = s.split('').sort();
4+
// const tSort = t.split('').sort();
5+
// return JSON.stringify(sSort) === JSON.stringify(tSort);
6+
7+
// 시간 복잡도 O(n), 공간복잡도 O(1)
8+
if (s.length !== t.length) return false;
9+
10+
const count = new Array(26).fill(0);
11+
for (let i=0; i<s.length; i++) {
12+
count[s.charCodeAt(i) - 97]++;
13+
count[t.charCodeAt(i) - 97]--;
14+
}
15+
16+
return count.every(c => c === 0);
17+
};

0 commit comments

Comments
 (0)