Skip to content

Commit c128958

Browse files
committed
Time: 11 ms (82.6%), Space: 20.6 MB (77.57%) - LeetHub
1 parent 4b3ac93 commit c128958

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# time complexity: O(nlogn * n)
2+
# space complexity: O(n)
3+
from collections import defaultdict
4+
from typing import List
5+
6+
7+
class Solution:
8+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
9+
wordMap = defaultdict(list)
10+
for word in strs:
11+
key = ''.join(sorted(word))
12+
wordMap[key].append(word)
13+
return [row for row in wordMap.values()]
14+
15+
16+
strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
17+
print(Solution().groupAnagrams(strs))
18+
strs = [""]
19+
print(Solution().groupAnagrams(strs))
20+
strs = ["a"]
21+
print(Solution().groupAnagrams(strs))

0 commit comments

Comments
 (0)