Lint 171. Anagrams

2019-10-10  本文已影响0人  Mree111

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
                

上一篇 下一篇

猜你喜欢

热点阅读