js实现字母异位词分组
输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
js
function groupAnagrams(strs) {
const map = new Map();
for (const str of strs) {
const sortedStr = str.split('').sort().join('');
console.log(sortedStr, 'sortedStr')
if (!map.has(sortedStr)) {
map.set(sortedStr, []);
}
map.get(sortedStr).push(str);
}
return Array.from(map.values());
}
const arr = groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])