1
+ # Given an array of strings strs, group the
2
+ # anagrams
3
+ # together. You can return the answer in any order.
4
+
5
+
6
+ # Example 1:
7
+ # Input: strs = ["eat","tea","tan","ate","nat","bat"]
8
+ # Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
9
+
10
+ # Explanation:
11
+
12
+ # There is no string in strs that can be rearranged to form "bat".
13
+ # The strings "nat" and "tan" are anagrams as they can be rearranged to
14
+ # form each other.
15
+ # The strings "ate", "eat", and "tea" are anagrams as they can be rearranged
16
+ # to form each other.
17
+
18
+ # Example 2:
19
+ # Input: strs = [""]
20
+ # Output: [[""]]
21
+
22
+ # Example 3:
23
+ # Input: strs = ["a"]
24
+ # Output: [["a"]]
25
+
26
+
27
+ # Constraints:
28
+
29
+ # 1 <= strs.length <= 104
30
+ # 0 <= strs[i].length <= 100
31
+ # strs[i] consists of lowercase English letters.
32
+
33
+
34
+ from typing import List
35
+ from collections import defaultdict
36
+ class Solution :
37
+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
38
+ mpp = defaultdict (list )
39
+
40
+ for str in strs :
41
+ key = '' .join (sorted (str ))
42
+ mpp [key ].append (str )
43
+
44
+ return list (mpp .values ())
0 commit comments