Skip to content

Commit 69969b7

Browse files
committed
[W5]
group-anagrams solution
1 parent 452da52 commit 69969b7

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

group-anagrams/sun912.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
TC: O(m*n)
3+
SC: O(26*n) -> O(n)
4+
"""
5+
6+
from collections import defaultdict
7+
8+
class Solution:
9+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
10+
result = defaultdict(list)
11+
12+
for word in strs:
13+
count = [0] * 26
14+
15+
for c in word:
16+
count[ord(c)-ord("a")] += 1
17+
result[tuple(count)].append(word)
18+
19+
return result.values()

0 commit comments

Comments
 (0)