Group Anagrams
Given an array of strings strs
, group all anagrams together into sublists. You may return the output in any order.
from collections import defaultdict
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
res = defaultdict(list)
for s in strs:
chars = [0] * 26
for char in s:
chars[ord(char) - ord('a')] += 1
res[tuple(chars)].append(s)
return list(res.values())
Solution | Time | Space |
---|---|---|
Sorting | ||
Hash Table |