We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5ac7e7d commit b885adcCopy full SHA for b885adc
group-anagrams/prograsshopper.py
@@ -0,0 +1,28 @@
1
+class Solution:
2
+ def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3
+ # sol 1 -> Time Exceed
4
+ from collections import defaultdict
5
+ word_dict = defaultdict(list)
6
+
7
+ for string in strs:
8
+ exist = False
9
+ for elem in word_dict.keys():
10
+ if sorted(elem) == sorted(string):
11
+ word_dict[elem].append(string)
12
+ exist = True
13
+ break
14
+ if not exist:
15
+ word_dict[string] = [string, ]
16
+ result = []
17
+ for elem in word_dict.values():
18
+ result.append(elem)
19
+ return result
20
21
+ # sol 2
22
+ # Time Complexity O(mn)
23
+ result = defaultdict(list)
24
25
26
+ key = "".join(sorted(string))
27
+ result[key].append(string)
28
+ return list(result.values())
0 commit comments