# JavaScript - CodeKata #18 ๐

devmoonshยท2020๋ 9์ 30์ผ
0

๋ชฉ๋ก ๋ณด๊ธฐ
43/48

## 1. Question

๋ค์๊ณผ ๊ฐ์ด 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)/

Junior front-end developer