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 4273f10 commit 6f7ed62Copy full SHA for 6f7ed62
group-anagrams/devyejin.py
@@ -0,0 +1,21 @@
1
+from typing import List
2
+from collections import Counter, defaultdict
3
+
4
+# class Solution:
5
+# def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
6
+#
7
+# dict_anagrams = defaultdict(list)
8
+# for idx, word in enumerate(strs):
9
+# key = tuple(sorted(Counter(word).items()))
10
+# dict_anagrams[key].append(word)
11
12
+# return list(dict_anagrams.values())
13
+class Solution:
14
+ def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
15
16
+ dict_anagrams = defaultdict(list)
17
+ for word in strs:
18
+ key = "".join(sorted(word))
19
+ dict_anagrams[key].append(word)
20
21
+ return list(dict_anagrams.values())
0 commit comments