Skip to content

Commit 3aa6b3b

Browse files
committed
valid-anagram 풀이1 #218
1 parent 8e4ca06 commit 3aa6b3b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

valid-anagram/seungseung88.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 시간복잡도: O(n)
3+
* - 첫번째 for문 O(n)
4+
* - 두번째 for문 최대 O(n)
5+
* 공간복잡도: O(n)
6+
* - count O(n)
7+
*/
8+
9+
const isAnagram = (s, t) => {
10+
if (s.length !== t.length) return false;
11+
12+
const count = {};
13+
14+
for (let i = 0; i < s.length; i += 1) {
15+
count[s[i]] = (count[s[i]] || 0) + 1;
16+
count[t[i]] = (count[t[i]] || 0) - 1;
17+
}
18+
19+
for (const i of Object.values(count)) {
20+
if (i !== 0) return false;
21+
}
22+
23+
return true;
24+
};

0 commit comments

Comments
 (0)