Skip to content

Commit e9eb1c4

Browse files
committed
group anagrams solution
1 parent 8f7a907 commit e9eb1c4

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

group-anagrams/jylee2033.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3+
anagram_map = {}
4+
for word in strs:
5+
key = "".join(sorted(word))
6+
if key not in anagram_map:
7+
anagram_map[key] = [word]
8+
else:
9+
anagram_map[key].append(word)
10+
11+
return list(anagram_map.values())
12+
13+
# Time Complexity: O(N * K log K), N - number of strings, K - maximum length of a string (for sorting)
14+
# Space Complexity: O(N * K)

0 commit comments

Comments
 (0)