forked from sammorozov/1337Code_tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path49. Group Anagrams.py
54 lines (31 loc) · 1.04 KB
/
49. Group Anagrams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from collections import Counter
from typing import *
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
cntrs = [Counter(s) for s in strs]
big_smoke = []
out = dict()
for i, j in zip(strs, cntrs):
out[i] = j
for string in strs:
temp = []
cnt = Counter(string)
for angrm, big_cnt in out.items():
if big_cnt == cnt:
temp.append(angrm)
big_smoke.append(temp)
return (big_smoke)
print(Solution().groupAnagrams(strs = ["eat","tea","tan","ate","nat","bat"]))
'''
мега ГЕНИАЛЬНОЕ РЕШЕНИЕ!!!
'''
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
dict = {}
for s in strs:
so ="".join(sorted(s))
if so not in dict:
dict[so] = [s]
else:
dict[so].append(s)
return dict.values()