美文网首页
Lint 171. Anagrams

Lint 171. Anagrams

作者: Mree111 | 来源:发表于2019-10-10 12:28 被阅读0次

    Description

    Given an array of strings, return all groups of strings that are anagrams.

    Solution

    class Solution:
        """
        @param strs: A list of strings
        @return: A list of strings
        """
        def anagrams(self, strs):
            # write your code here
            ana_dict = {}
            for s in strs:
                if len(s) !=0:
                    res = "".join(sorted(s))
                else:
                    res = s
                if res not in ana_dict:
                    ana_dict[res] = [s]
                else:
                    ana_dict[res].append(s)
            res = []
            for k,v in ana_dict.items():
                if len(v) >1:
                   res+=v
            return res
                    
    
    

    相关文章

      网友评论

          本文标题:Lint 171. Anagrams

          本文链接:https://www.haomeiwen.com/subject/rakopctx.html