Skip to content

Commit 04e7e83

Browse files
committed
feat: valid anagram
1 parent e0d7d23 commit 04e7e83

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

valid-anagram/anniemon.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 시간 복잡도:
3+
* s와 t의 길이만큼 각 문자의 카운트를 기록하고 이를 확인하므로, 시간 복잡도는 O(n)
4+
* 공간 복잡도:
5+
* 카운트 객체는 최대 s와 t의 길이만큼 공간을 차지하므로, 공간 복잡도는 O(n)
6+
*/
7+
/**
8+
* @param {string} s
9+
* @param {string} t
10+
* @return {boolean}
11+
*/
12+
var isAnagram = function (s, t) {
13+
if (s.length !== t.length) {
14+
return false;
15+
}
16+
17+
const count = {};
18+
for (let i = 0; i < s.length; i++) {
19+
count[s[i]] = (count[s[i]] || 0) + 1;
20+
count[t[i]] = (count[t[i]] || 0) - 1;
21+
}
22+
23+
for (const key in count) {
24+
if (count[key] !== 0) {
25+
return false;
26+
}
27+
}
28+
return true;
29+
};

0 commit comments

Comments
 (0)