다음과 같이 input이 주어졌을 때,같은 알파벳으로 이루어진 단어끼리 묶어주세요.
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
output에서 순서는 상관없습니다.
const groupAnagrams = strs => {
const map = {}
for(let i of strs){
const key = [...i].sort().join('')//aet
if(!map[key]){
map[key] = [];
}
map[key].push(i)
}
return Object.values(map)
}
console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]));