-
Notifications
You must be signed in to change notification settings - Fork 0
/
N49_group_anagrams.py
23 lines (19 loc) · 1.04 KB
/
N49_group_anagrams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
def groupAnagrams(self, strs: list) -> list:
anagrams_dict = dict()
for st in strs:
st_s = ''.join(sorted(st))
if st_s not in anagrams_dict:
anagrams_dict[st_s] = []
anagrams_dict[st_s].append(st)
return list(anagrams_dict.values())
if __name__ == '__main__':
sol = Solution()
assert sol.groupAnagrams(["bdddddddddd", "bbbbbbbbbbc"]) == [["bdddddddddd"], ["bbbbbbbbbbc"]]
assert sol.groupAnagrams(["bzzz", "zzzc"]) == [["bzzz"], ["zzzc"]]
assert sol.groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]) == [['eat', 'tea', 'ate'], ['tan', 'nat'],
['bat']]
assert sol.groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat", ""]) == [['eat', 'tea', 'ate'], ['tan', 'nat'],
['bat'], ['']]
assert sol.groupAnagrams([""]) == [[""]]
assert sol.groupAnagrams(["a"]) == [["a"]]