๋ค์๊ณผ ๊ฐ์ด input์ด ์ฃผ์ด์ก์ ๋,๊ฐ์ ์ํ๋ฒณ์ผ๋ก ์ด๋ฃจ์ด์ง ๋จ์ด๋ผ๋ฆฌ ๋ฌถ์ด์ฃผ์ธ์.
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
output์์ ์์๋ ์๊ด์์ต๋๋ค.
const groupAnagrams = strs => {
let results = [];
for (let i = 0; i < strs.length; i++) {
let temp = [strs[i]];
for (let j = i+1; j < strs.length; j++) {
let isTrue = true;
for (let k = 0; k < strs[j].length; k++) {
if (!strs[i].includes(strs[j][k])) {
isTrue = false;
break;
}
}
if (isTrue) {
temp.push(strs[j]);
strs[j] = ''
}
}
let container = []
while (temp.length) {
if (temp[0] !== '') {
container.push(temp.shift());
} else {
temp.shift();
}
}
if (container.length) {
results.push(container)
}
}
return results;
}
console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]))
Ref.
https://choiseungyoun.github.io/posts/TIL-Day31:-Code-Kata-18(JS-&-PY)/