-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path49.py
38 lines (34 loc) · 1.27 KB
/
49.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
37
38
__________________________________________________________________________________________________
sample 96 ms submission
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
if not strs:
return [[]]
if len(strs) == 1:
return [strs]
anagrams = {}
for s in strs:
key = ''.join(sorted(s))
val = anagrams.get(key, [])
val.append(s)
anagrams[key] = val
res = []
for entry in anagrams.items():
res.append(entry[1])
return res
__________________________________________________________________________________________________
sample 15312 kb submission
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
resultsObj = {}
results = []
for word in strs:
sortedWord = ''.join(sorted(word))
if not sortedWord in resultsObj:
resultsObj[sortedWord] = [word]
else:
resultsObj[sortedWord].append(word)
for k in resultsObj:
results.append(resultsObj[k])
return results
__________________________________________________________________________________________________