對(duì)一串字符串中的每個(gè)字符按照出現(xiàn)次數(shù)重排序,出現(xiàn)次數(shù)相同的話(huà)先后順序隨意,e.g.,
Input: s = "Aabb"
Output: "bbAa"
直接用python的Counter的most_common按出現(xiàn)次數(shù)多到少排序字符
1 #451
2 #Runtime: 133 ms
3 #Memory Usage: 15.9 MB
4
5 class Solution(object):
6 def frequencySort(self, s):
7 """
8 :type s: str
9 :rtype: str
10 """
11 return ''.join(ch*n for ch, n in Counter(s).most_common())