451. Sort Characters By Frequency
- Sort Characters By Frequency python solution
题目描述
Given a string, sort it in decreasing order based on the frequency of characters.
解析
解题思路比较简单,见代码
class Solution(object):
def frequencySort(self, s):
import collections
if not s:
return ""
count_s = collections.Counter(s)
counter = count_s.most_common()
rs = ''
for i in counter:
rs += i[0] * i[1]
return rs
Reference
https://leetcode.com/problems/sort-characters-by-frequency/discuss/93411/Two-ways-of-Python-Solution-easy-to-understand.