[tag] hash map, dict
- Hashmap/defaultdict with keys being strings and values being lists.
'''
- use a hash map:
- key: sorted single word
- value: the index of that word
'''
# abc, [1, 2, 3]
# abc, ['cba', 'abs', 'acb']
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
str_dict = defaultdict(list)
ans = []
for i, one_str in enumerate(strs):
one_str_ordered = ''.join(sorted(one_str))
str_dict[one_str_ordered].append(one_str)
for key, value in str_dict.items():
ans.append(value)
return ans