Skip to content

Commit e956d7d

Browse files
committed
Solve: groupAnagrams
1 parent 17da131 commit e956d7d

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

group-anagrams/daehyun99.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
"""

0 commit comments

Comments
 (0)