Skip to content

Commit f176b79

Browse files
authored
Update Group Anagrams - Leetcode 49.py
1 parent 4dfbc49 commit f176b79

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Group Anagrams - Leetcode 49/Group Anagrams - Leetcode 49.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
115
from collections import defaultdict
216
class Solution:
317
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:

0 commit comments

Comments
 (0)