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
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步