Skip to content

Commit af186fa

Browse files
committed
valid anagram solution
1 parent bcde25c commit af186fa

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

โ€Žvalid-anagram/limlimjo.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
var isAnagram = function (s, t) {
7+
// ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋ฉด false
8+
if (s.length !== t.length) {
9+
return false;
10+
}
11+
12+
// s ๋นˆ๋„์ˆ˜, t ๋นˆ๋„์ˆ˜
13+
const countS = {};
14+
const countT = {};
15+
16+
// ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•˜๊ธฐ
17+
for (let i = 0; i < s.length; i++) {
18+
countS[s[i]] = (countS[s[i]] || 0) + 1;
19+
countT[t[i]] = (countT[t[i]] || 0) + 1;
20+
}
21+
22+
// ๋‘ ๊ฐ์ฒด ๋™์ผํ•˜๋ฉด true, ์•„๋‹ˆ๋ฉด false
23+
for (let char in countS) {
24+
if (countS[char] !== countT[char]) {
25+
return false;
26+
}
27+
}
28+
return true;
29+
};
30+
31+
// ์‹œ๊ฐ„๋ณต์žก๋„: for๋ฌธ ์ˆœํšŒ ๋น„๊ตํ•˜๋ฏ€๋กœ O(n)
32+
// ๊ณต๊ฐ„๋ณต์žก๋„: countS, countT ์ตœ๋Œ€ n๊ฐœ์˜ ํ‚ค ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ O(n)

0 commit comments

Comments
ย (0)