-
Notifications
You must be signed in to change notification settings - Fork 6
/
leetcode-groupAnagrams.py
36 lines (33 loc) · 1.02 KB
/
leetcode-groupAnagrams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
ans=[]
dict1 = {}
if strs==[""]:
return [[""]]
elif len(strs) == 1:
return [strs]
else :
for word in strs:
sorted_word = "".join(sorted(word))
if sorted_word not in dict1:
dict1[sorted_word]=[word]
else:
dict1[sorted_word].append(word)
for words in dict1.values():
ans.append(words)
return ans
dict1= {}
for word in strs:
sorted_word = "".join(sorted(word))
if sorted_word not in dict1:
dict1[sorted_word]=[word]
else:
dict1[sorted_word].append(word)
result = []
for words in dict1.values():
result.append(words)
return result