给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
看到题目想到对字符串排序不难,但是需要对结果进行去重,去重方法其实没想到,需要使用字典去重,然后将字段的values获得最终结果。
python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
if not strs:
return [[]]
d = {}
for s in strs:
char = ''.join(sorted(s))
if char not in d:
d[char] = []
d[char].append(s)
return list(d.values())