classSolution:defgroupAnagrams(self,strs: List[str]) -> List[List[str]]: ans = collections.defaultdict(list)#创建defaultdictfor s in strs: ans[tuple(sorted(s))].append(s)#注意将sorted返回的结果转成tuplereturnlist(ans.values())#ans的value是dict_values类型,注意转成list
classSolution:defgroupAnagrams(self,strs: List[str]) -> List[List[str]]: ans = collections.defaultdict(list)for s in strs: count = [0] *26#词频记录for c in s:#统计词频 count[ord(c)-ord('a')]+=1 ans[tuple(count)].append(s)#注意count要转成tuplereturnlist(ans.values())