面试题 10.02. 变位词组



注:sorted()可以排字符串。

class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        mydict = {}
        for item in strs:
            temp = ''.join(sorted(item))
            if temp in mydict.keys():
                mydict[temp].append(item)
            else:
                mydict[temp]=[item]
        res = []
        for v in mydict.values():
            res.append(v)
        return res
原文地址:https://www.cnblogs.com/panweiwei/p/14024862.html