-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
451.py
35 lines (33 loc) · 1.15 KB
/
451.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
__________________________________________________________________________________________________
sample 32 ms submission
class Solution:
def frequencySort(self, s: str) -> str:
a = set(s)
a = [a*s.count(a) for a in a]
a = sorted(a, key = lambda x: len(x), reverse = True)
return ''.join(a)
__________________________________________________________________________________________________
sample 13584 kb submission
class Solution:
def frequencySort(self, s: str) -> str:
freq = dict()
for c in s:
if c in freq:
freq[c]+=1
else:
freq[c]=1
freq_r = dict()
for k,v in freq.items():
if v in freq_r:
freq_r[v].append(k)
else:
freq_r[v]=[k]
freq_list = list(freq_r.keys())
freq_list.sort(reverse=True)
output = ""
for f in freq_list:
for c in freq_r[f]:
for i in range(f):
output+=c
return output
__________________________________________________________________________________________________