Skip to content

Commit 8b4c4ee

Browse files
committed
group anagrams solution
1 parent 3ac2ea0 commit 8b4c4ee

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

group-anagrams/dolphinflow86.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 1) Group words by their sorted form using defaultdict. While iterating the strs, sort each word and append original word to the corresponding list. After then convert dict to 2 dimensional list and return the list.
2+
# TC: O(N*LlogL) where N is length of strs, L is max length of a word.
3+
# SC: O(N*L)
4+
5+
class Solution:
6+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
7+
groups = defaultdict(list)
8+
9+
for str in strs:
10+
sorted_str = "".join(sorted(str))
11+
groups[sorted_str].append(str)
12+
13+
return list(groups.values())

0 commit comments

Comments
 (0)