문제 링크 : Group Anagrams
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function(strs) {
const sortStrs = strs.map(e => {
return e.split('').sort().join('')
})
const mapSortStr = new Map();
sortStrs.forEach(e => {
mapSortStr.has(e) ? mapSortStr.set(e, mapSortStr.get(e) + 1) : mapSortStr.set(e, 1)
})
const mapKeys = [...mapSortStr.keys()]
const result = Array.from(Array(mapKeys.length), () => [])
sortStrs.forEach( (e,i) => {
result[mapKeys.indexOf(e)].push(strs[i])
})
return result.sort( (a,b) => a.length - b.length)
};
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function(strs) {
let map = {}
for(let str of strs) {
let s = str.split('').sort().join('')
if(!map[s]) map[s] = []
map[s].push(str)
}
return Object.values(map)
};