将字符串排序
然后制作hash表
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
newlist = []
for thisstr in strs:
temp = ''
thisstr = sorted(thisstr)
for letter in thisstr:
temp += letter
newlist.append(temp)
hashdict = {}
for i,thisstr in enumerate(newlist):
if thisstr not in hashdict:
hashdict[thisstr] = [strs[i]]
elif thisstr in hashdict:
hashdict[thisstr].append(strs[i])
res = []
for key in hashdict:
res.append(hashdict[key])
return res