Group Anagrams
Description
Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:
- All inputs will be in lowercase.
- The order of your output does not matter.
Solution(javascript)
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function(strs) {
let map = new Map();
for (let str of strs){
const s = sort(str);
let l = map.get(s) || [];
l.push(str);
map.set(s, l);
}
return Array.from(map.values());
};
const sort = function(str) {
return str
.split("")
.sort()
.join("");
};