Posted on 2022-12-03 14:32
Uriel 閱讀(40)
評論(0) 編輯 收藏 引用 所屬分類:
閑來無事重切Leet Code 、
大水題
對一串字符串中的每個字符按照出現次數重排序,出現次數相同的話先后順序隨意,e.g.,
Input: s = "Aabb"
Output: "bbAa"
直接用python的Counter的most_common按出現次數多到少排序字符
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())