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)