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
网友评论