anagrams

anagram,bing词典上的解释是颠倒字母而成的词句,例如"dog""god"就是一对anagram;

题目的大致意思是:给出一个由string组成的list,返回其中所有的是anagram的string

以下是python代码

class Solution:
    # @param strs, a list of strings
    # @return a list of strings
    def anagrams(self, strs):
        hashtable={}
        strs_temp=[]
        res=[]
        for s in strs:
            temp=''.join(sorted(s)) #
            strs_temp.append(temp)
            #print temp
            if(hashtable.get(temp)):
                hashtable[temp]+=1
            else:
                hashtable[temp]=1
        #print strs_temp
        for i in range(len(strs_temp)):
            if hashtable[strs_temp[i]]>1:
                res.append(strs[i])
        return res

原文地址:https://www.cnblogs.com/iois/p/4295556.html