解题思路
哈希表:字典
字典的值就是列表,把结果直接存进去就行了
python 代码
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
res = []
dd = {}
for i in strs:
a = str(sorted(i))
if a not in dd:
dd[a]=[]
if a in dd:
dd[a].append(i)
for i in dd.values():
res.append(i)
return res
s = Solution()
result = s.groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])
print(result)