MediumLeetCode #49Arrays & Hashing
Group Anagrams
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
Constraints
1 <= strs.length <= 10^4, 0 <= strs[i].length <= 100
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
1 <= strs.length <= 10^4, 0 <= strs[i].length <= 100
Use sorted string as key in hash map. All anagrams will have the same sorted representation.
def groupAnagrams(strs):
groups = defaultdict(list)
for s in strs:
key = tuple(sorted(s))
groups[key].append(s)
return list(groups.values())