We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2eddee7 commit 9f84fe0Copy full SHA for 9f84fe0
Twitter/Problem#386.py
@@ -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