Skip to content

Commit 9f84fe0

Browse files
committed
Sort Characters By Frequency
1 parent 2eddee7 commit 9f84fe0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Twitter/Problem#386.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Given a string, sort it in decreasing order based on the frequency of characters.
3+
If there are multiple possible solutions, return any of them.
4+
For example, given the string tweet, return tteew. eettw would also be acceptable.
5+
"""
6+
def frequencySort(s: str) -> str:
7+
d1, d2 = {}, {}
8+
# counter of alphabets
9+
for c in s:
10+
d1[c] = d1.get(c, 0) + 1
11+
12+
# make count as key and alphabets as value
13+
for k,v in d1.items():
14+
d2[v] = d2.get(v, '') + k*v
15+
16+
res = ''
17+
for i in range(len(s), -1, -1):
18+
if i in d2:
19+
res += d2[i]
20+
return res

0 commit comments

Comments
 (0)