Skip to content

Commit 7a42554

Browse files
author
lightmen
committed
add sort-characters-by-frequency.py
1 parent 5b11030 commit 7a42554

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def frequencySort(self, s):
3+
"""
4+
:type s: str
5+
:rtype: str
6+
"""
7+
d = {}
8+
l = []
9+
for c in s:
10+
d[c] = d.get(c, 0) + 1
11+
12+
for c in d:
13+
index = 0
14+
while index < len(l):
15+
if d[l[index]] < d[c]:
16+
break
17+
index += 1
18+
l.insert(index, c)
19+
20+
res = ""
21+
for c in l:
22+
res += c * d[c]
23+
24+
return res

0 commit comments

Comments
 (0)