๋ฌธ์ ๋งํฌ
Valid Anagram - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
๋ ๋ฌธ์์ด์ด anagram์ธ์ง ํ๋ณํ๋ ๋ฌธ์ ์ด๋ค.
* anagram : ํ ๋ฌธ์์ด์ ๋จ์ด๋ฅผ ๋ชจ๋ ์ฌ์ฉํด์ ๋ค๋ฅธ ๋ฌธ์์ด์ ๋ง๋๋ ๋์ด
์ ๊ทผ ๋ฐฉ๋ฒ
๋ ๋ฌธ์์ด์ ๋ฐฐ์ด๋ก ๋ณํํ๊ณ ์ ๋ ฌํ ๋ค, ๋ฌธ์์ด๋ก ๋ณํํ์ฌ ๊ฐ์์ง ๊ตฌํ์๋ค.
โป ๋ฐฐ์ด์ ์ฐธ์กฐํ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ด๊ธฐ ๋๋ฌธ์ ๋ด์ฉ์ด ๊ฐ์๋ ์ฃผ์๊ฐ ๋ฌ๋ผ ๊ทธ๋๋ก ๋น๊ต ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
๋ฐฐ์ด์ ๋ฌธ์์ด๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ
1. JSON.stringify๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌธ์์ด๋ก ๋ณํํ๊ณ ๋น๊ต
2. join("")์ ์ฌ์ฉํ์ฌ ๋ฌธ์์ด๋ก ๋ณํํ์ฌ ๋น๊ต
3. toString()์ ์ฌ์ฉํ์ฌ ๋ฌธ์์ด๋ก ๋ณํํ์ฌ ๋น๊ต
1, 2, 3์ ์๋์์ ๋ณ๋ค๋ฅธ ์ฐจ์ด๋ฅผ ๋ณด์ด์ง ์์๋ค.
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
// return s.split("").sort().join("") === t.split("").sort().join("")
// return s.split("").sort().toString() === t.split("").sort().toString()
return JSON.stringify(s.split("").sort()) === JSON.stringify(t.split("").sort())
};
'์ฝ๋ฉํ ์คํธ > LeetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] Unique Paths (0) | 2022.08.01 |
---|---|
[LeetCode] Find And Replace Pattern (0) | 2022.07.29 |
[LeetCode] Median Of Two Sorted Arrays (0) | 2022.07.25 |
[LeetCode] Search A 2d Matrix II (0) | 2022.07.24 |
[LeetCode] Number Of Matching Subsequences (0) | 2022.07.20 |