字谜分组
给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
说明:
所有输入均为小写字母。
不考虑答案输出的顺序。
输入: ["eat", "tea", "tan", "ate", "nat", "bat"],
输出:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
strsmap = {}
result = []
for string in strs:
tmp = ''.join(sorted(string))
if tmp in strsmap:
strsmap[tmp].append(string)
else:
strsmap[tmp] = [string]
for strlist in strsmap.values():
result.append(strlist)
return result