Skip to content

Commit ad62cd8

Browse files
committed
valid anagram solution
1 parent a70f1b6 commit ad62cd8

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

valid-anagram/juhui-jeong.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 시간 복잡도(Time Complexity): O(n)
2+
// 공간 복잡도(Space Complexity): O(1)
3+
function isAnagram(s: string, t: string): boolean {
4+
if (s.length !== t.length) return false;
5+
6+
const count = new Array(26).fill(0);
7+
8+
for (let i = 0; i < s.length; i++) {
9+
count[s.charCodeAt(i) - 97]++;
10+
count[t.charCodeAt(i) - 97]--;
11+
}
12+
13+
return count.every((n) => n === 0);
14+
}
15+
16+
/*
17+
// 첫 번째 풀이
18+
// 시간 복잡도(Time Complexity): O(n log n)
19+
// 공간 복잡도(Space Complexity): O(n)
20+
21+
function isAnagram(s: string, t: string): boolean {
22+
let baseString = s.split('').sort().toString();
23+
let targetString = t.split('').sort().toString();
24+
25+
return baseString === targetString ? true : false;
26+
}
27+
*/

0 commit comments

Comments
 (0)