# 49. 字母异位词分组

// 给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。

var groupAnagrams = function(strs) {
  const map = new Map();

  for (const str of strs) {
    let array = Array.from(str);
    array = array.sort();
    const key = array.toString();
    const list = map.get(key) ? map.get(key) : [];
    list.push(str);
    map.set(key, list); 
  }

  return [...map.values()];
};

console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]));
// [["ate", "eat", "tea"], ["nat", "tan"], ["bat"]]
// console.log(groupAnagrams(["", "b", ""]));
// console.log(groupAnagrams(["ddddddddddg", "dgggggggggg"]));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Last Updated: 6/27/2023, 7:40:45 PM