{ "question_id": 49, "question_title": "字母异位词分组", "difficulty": "中等", "question_content": "

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

\n

示例:

\n
输入:[eat", "tea", "tan", "ate", "nat", "bat"]
输出:
[[ate","eat","tea"],["nat","tan"],["bat"]]
\n

说明:

\n", "topic_link": "https://bbs.csdn.net/topics/600470799", "cpp": "#include \nusing namespace std;\nclass Solution\n{\npublic:\n\tvector> groupAnagrams(vector &strs)\n\t{\n\t\tvector> res;\n\t\tunordered_map> ht;\n\t\tfor (const auto &str : strs)\n\t\t{\n\t\t\tint counts[26] = {0};\n\t\t\tfor (char c : str)\n\t\t\t{\n\t\t\t\tcounts[c - 'a']++;\n\t\t\t}\n\t\t\tstring key;\n\t\t\tfor (int i : counts)\n\t\t\t{\n\t\t\t\tkey.push_back('#');\n\t\t\t\tkey.push_back(i + '0');\n\t\t\t}\n\t\t\tht[key].push_back(str);\n\t\t}\n\t\tfor (const auto &t : ht)\n\t\t{\n\t\t\tres.push_back(t.second);\n\t\t}\n\t\treturn res;\n\t}\n};", "java": "\nimport java.util.*;\n\npublic class GroupAnagrams {\n\n\tpublic List> groupAnagrams(String[] strs) {\n\t\tHashMap> map = new HashMap<>();\n\t\tfor (String str : strs) {\n\t\t\tchar[] cs = str.toCharArray();\n\t\t\tArrays.sort(cs);\n\n\t\t\tString key = String.valueOf(cs);\n\t\t\tif (!map.containsKey(key)) {\n\t\t\t\tmap.put(key, new ArrayList<>());\n\t\t\t}\n\t\t\tmap.get(key).add(str);\n\t\t}\n\t\treturn new ArrayList(map.values());\n\t}\n\n\tpublic List> groupAnagrams2(String[] strs) {\n\t\tif (strs.length <= 0) {\n\t\t\treturn new ArrayList<>();\n\t\t}\n\t\tHashMap> map = new HashMap<>();\n\t\tfor (String str : strs) {\n\t\t\tchar[] cs = str.toCharArray();\n\t\t\tint[] count = new int[26];\n\t\t\tfor (char c : cs) {\n\t\t\t\t++count[c - 'a'];\n\t\t\t}\n\t\t\tStringBuilder s = new StringBuilder(\"\");\n\t\t\tfor (int num : count) {\n\t\t\t\ts.append(num);\n\t\t\t}\n\n\t\t\tString key = String.valueOf(s);\n\n\t\t\tif (!map.containsKey(key)) {\n\t\t\t\tmap.put(key, new ArrayList<>());\n\t\t\t}\n\t\t\tmap.get(key).add(str);\n\t\t}\n\t\treturn new ArrayList(map.values());\n\t}\n}\n", "js": "", "python": "class Solution(object):\n\tdef groupAnagrams(self, strs):\n\t\tstrs.sort()\n\t\thash = {}\n\t\tfor s in strs:\n\t\t\tkey = self.hash_key(s)\n\t\t\ttry:\n\t\t\t\thash[key].append(s)\n\t\t\texcept KeyError:\n\t\t\t\thash[key] = [s]\n\t\treturn hash.values()\n\tdef hash_key(self, s):\n\t\ttable = [0] * 26\n\t\tfor ch in s:\n\t\t\tindex = ord(ch) - ord('a')\n\t\t\ttable[index] += 1\n\t\treturn str(table)\n# %%\ns = Solution()\nprint(s.groupAnagrams(strs = [\"eat\", \"tea\", \"tan\", \"ate\", \"nat\", \"bat\"]))", "status": 1, "keywords": "哈希表,字符串,排序", "license": { "cpp": "https://github.com/begeekmyfriend/leetcode", "python": "https://github.com/qiyuangong/leetcode", "java": "https://github.com/zhangyu345293721/leetcode" }, "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/48/48_cpp.ipynb?type=file", "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/48/48_python.ipynb?type=file", "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/48/48_java.ipynb?type=file" }, "notebook_enable": 1 }