forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0049.py
More file actions
24 lines (20 loc) · 636 Bytes
/
Copy path0049.py
File metadata and controls
24 lines (20 loc) · 636 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
strs_map = {}
result = []
for string in strs:
tmp = ''.join(sorted(string))
if tmp in strs_map:
strs_map[tmp].append(string)
else:
strs_map[tmp] = [string]
for str_list in strs_map.values():
result.append(str_list)
return result
if __name__ == "__main__":
strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
print(Solution().groupAnagrams(strs))