-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path451.linningmii.py
42 lines (31 loc) · 913 Bytes
/
451.linningmii.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
class CountItem:
def __init__(self, key, count):
self.key = key
self.count = count
class Solution:
def sort_method(self, item):
return item.count
def frequencySort(self, s):
"""
:type s: str
:rtype: str
"""
d_dict = {}
for i in range(len(s)):
if d_dict.get(s[i]):
d_dict[s[i]] += 1
else:
d_dict[s[i]] = 1
keys = d_dict.keys()
sorted_list = []
for key in keys:
sorted_list.append(CountItem(key, d_dict.get(key)))
sorted_list.sort(key=self.sort_method, reverse=True)
result = ''
for item in sorted_list:
for char in range(item.count):
result += item.key
return result
solution = Solution()
print(solution.frequencySort("cccaaa"))
print(solution.frequencySort("tree"))