Group Anagrams

问题:给定一个由字符串构成的列表,将列表中的字符串分组并输出,字符串的构成字母完全相同则分为一组

示例:

  输入:["abc","ten","mbr","nte","cba","bca","brm"]

  输出:[["abc","cba","bca"],["ten","nte"],["mbr","brm"]]

python代码:

class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        dic = {}
        for i in strs:
            key = "".join(sorted(i))
            dic.setdefault(key,[]).append(i)
        return list(dic.values())

 

转载于:https://www.cnblogs.com/wenqinchao/p/10933137.html

你可能感兴趣的:(Group Anagrams)