链接:
题目:
给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
示例:
输入: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”],
输出: [ [“ate”,“eat”,“tea”], [“nat”,“tan”], [“bat”] ]
我的解法:每个组合sort之后给一个key
class Solution(object):
def groupAnagrams(self, strs):
dic = {}
for str in strs:
s_str = ''.join(sorted(str))
if s_str in dic:
dic[s_str].append(str)
else:
dic[s_str] = [str]
return dic.values()