Python | Leetcode Python题解之第451题根据字符出现频率排序
题目:
题解:
class Solution:def frequencySort(self, s: str) -> str:count = {}for c in s:count[c] = count.get(c, 0) + 1items = [(-val, key) for key, val in count.items()]heapq.heapify(items)res = ""while items:val, key = heapq.heappop()res += key * (-val)return res