# [面试题 10.02. 变位词组](https://leetcode.cn/problems/group-anagrams-lcci) [English Version](/lcci/10.02.Group%20Anagrams/README_EN.md) ## 题目描述 <!-- 这里写题目描述 --> <p>编写一种方法,对字符串数组进行排序,将所有变位词组合在一起。变位词是指字母相同,但排列不同的字符串。</p> <p><strong>注意:</strong>本题相对原题稍作修改</p> <p><strong>示例:</strong></p> <pre><strong>输入:</strong> <code>["eat", "tea", "tan", "ate", "nat", "bat"]</code>, <strong>输出:</strong> [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ]</pre> <p><strong>说明:</strong></p> <ul> <li>所有输入均为小写字母。</li> <li>不考虑答案输出的顺序。</li> </ul> ## 解法 <!-- 这里可写通用的实现逻辑 --> 遍历字符串,将每个字符串按照字符字典序排序后得到一个新的字符串,将相同的新字符串放在哈希表的同一个 key 对应 value 列表中。 | key | value | | ------- | ----------------------- | | `"aet"` | `["eat", "tea", "ate"]` | | `"ant"` | `["tan", "nat"] ` | | `"abt"` | `["bat"] ` | 最后返回哈希表的 value 列表即可。 <!-- tabs:start --> ### **Python3** <!-- 这里可写当前语言的特殊实现逻辑 --> ```python class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: chars = defaultdict(list) for s in strs: k = ''.join(sorted(list(s))) chars[k].append(s) return list(chars.values()) ``` ### **Java** <!-- 这里可写当前语言的特殊实现逻辑 --> ```java class Solution { public List<List<String>> groupAnagrams(String[] strs) { Map<String, List<String>> chars = new HashMap<>(); for (String s : strs) { char[] t = s.toCharArray(); Arrays.sort(t); String k = new String(t); chars.computeIfAbsent(k, key -> new ArrayList<>()).add(s); } return new ArrayList<>(chars.values()); } } ``` ### **C++** ```cpp class Solution { public: vector<vector<string>> groupAnagrams(vector<string>& strs) { unordered_map<string, vector<string>> chars; for (auto s : strs) { string k = s; sort(k.begin(), k.end()); chars[k].emplace_back(s); } vector<vector<string>> res; for (auto it = chars.begin(); it != chars.end(); ++it) { res.emplace_back(it->second); } return res; } }; ``` ### **Go** ```go func groupAnagrams(strs []string) [][]string { chars := map[string][]string{} for _, s := range strs { key := []byte(s) sort.Slice(key, func(i, j int) bool { return key[i] < key[j] }) chars[string(key)] = append(chars[string(key)], s) } var res [][]string for _, v := range chars { res = append(res, v) } return res } ``` ### **TypeScript** ```ts function groupAnagrams(strs: string[]): string[][] { const map = new Map<string, string[]>(); for (const s of strs) { const k = s.split('').sort().join(); map.set(k, (map.get(k) || []).concat([s])); } return [...map.values()]; } ``` ### **Rust** ```rust use std::collections::HashMap; impl Solution { pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> { let mut map = HashMap::new(); for s in strs { let key = { let mut cs = s.chars().collect::<Vec<char>>(); cs.sort(); cs.iter().collect::<String>() }; map.entry(key).or_insert(vec![]).push(s); } map.into_values().collect() } } ``` ### **...** ``` ``` <!-- tabs:end -->