Solution 1
-
Get sorted chars array in each str.
-
Store the strs with the same chars array into the same dictionary item.
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
dic = {}
for s in strs:
chars = ''.join(sorted(s))
if chars in dic:
dic[chars].append(s)
else:
dic[chars] = [s]
res = []
for key in dic:
res.append(dic[key])
return res
Solution 2
-
Get the number of appearance of each characters in the array.
-
Use the
count
as key.def groupAnagrams(self, strs): ans = collections.defaultdict(list) for s in strs: ans[tuple(sorted(s))].append(s) return ans.values()
PREVIOUSSubsets
NEXTRotate Image