File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ from collections import defaultdict
2+ class Solution :
3+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
4+ groups = defaultdict (list )
5+
6+ for s in strs :
7+ count = [0 ] * 26
8+
9+ for c in s :
10+ count [ord (c )- ord ('a' )] += 1
11+
12+ groups [tuple (count )].append (s )
13+ return list (groups .values ())
14+
15+ """
16+ class Solution:
17+ def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
18+ def decode(s, key, base):
19+ for c in s:
20+ key[ord(c) - base] += 1
21+ result = ""
22+ for k in key:
23+ result += ' ' + str(k)
24+ return result
25+ keys = {}
26+ base_key = [0 for i in range(26)]
27+ base = ord("a")
28+
29+ for s in strs:
30+ key = decode(s, base_key.copy(), base)
31+ temp = keys.get(key, [])
32+ temp.append(s)
33+ keys[key] = temp
34+ results = []
35+ for val in keys.values():
36+ results.append(val)
37+ return results
38+ """
You can’t perform that action at this time.
0 commit comments