Skip to content

Commit

Permalink
valid anagram solution
Browse files Browse the repository at this point in the history
  • Loading branch information
limlimjo committed Dec 16, 2024
1 parent bcde25c commit af186fa
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions valid-anagram/limlimjo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
// ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋ฉด false
if (s.length !== t.length) {
return false;
}

// s ๋นˆ๋„์ˆ˜, t ๋นˆ๋„์ˆ˜
const countS = {};
const countT = {};

// ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•˜๊ธฐ
for (let i = 0; i < s.length; i++) {
countS[s[i]] = (countS[s[i]] || 0) + 1;
countT[t[i]] = (countT[t[i]] || 0) + 1;
}

// ๋‘ ๊ฐ์ฒด ๋™์ผํ•˜๋ฉด true, ์•„๋‹ˆ๋ฉด false
for (let char in countS) {
if (countS[char] !== countT[char]) {
return false;
}
}
return true;
};

// ์‹œ๊ฐ„๋ณต์žก๋„: for๋ฌธ ์ˆœํšŒ ๋น„๊ตํ•˜๋ฏ€๋กœ O(n)
// ๊ณต๊ฐ„๋ณต์žก๋„: countS, countT ์ตœ๋Œ€ n๊ฐœ์˜ ํ‚ค ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ O(n)

0 comments on commit af186fa

Please sign in to comment.