Friday, September 12, 2014

Leetcode: Anagrams @Python

class Solution:
    # @param strs, a list of strings
    # @return a list of strings
    def anagrams(self, strs):
        dic={}
        res=[]
        for word in strs:
            S_word=''.join(sorted(word))
            dic[S_word]=[word] if S_word not in dic else dic[S_word]+[word]
        for i in dic:
            if len(dic[i])>1:
                res+=dic[i]
        return res

No comments :

Post a Comment