All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 18, 2024
Last updated : June 18, 2024
Related Topics : Hash Table, String, Sorting, Heap (Priority Queue), Bucket Sort, Counting
Acceptance Rate : 73.16 %
class Solution:
def frequencySort(self, s: str) -> str:
cnt = Counter(s)
output = [(-1 * freq, val) for val, freq in cnt.items()]
heapq.heapify(output)
actualOutput = []
while output :
freq, val = heapq.heappop(output)
actualOutput.extend([val] * (freq * -1))
return ''.join(actualOutput)