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 4dfbc49 commit f176b79Copy full SHA for f176b79
Group Anagrams - Leetcode 49/Group Anagrams - Leetcode 49.py
@@ -1,3 +1,17 @@
1
+# Brute Force Solution
2
+class Solution:
3
+ def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
4
+ ans = defaultdict(list)
5
+ for s in strs:
6
+ key = ''.join(sorted(s))
7
+ ans[key].append(s)
8
+
9
+ return list(ans.values())
10
+# Time: O(n * (m log m))
11
+# Space: O(n * m)
12
+# n is the number of strings, m is the length of largest string
13
14
+# Optimal Solution
15
from collections import defaultdict
16
class Solution:
17
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
0 commit comments