Skip to content

Latest commit

 

History

History
42 lines (28 loc) · 1.1 KB

_451. Sort Characters By Frequency.md

File metadata and controls

42 lines (28 loc) · 1.1 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 18, 2024

Last updated : June 18, 2024


Related Topics : Hash Table, String, Sorting, Heap (Priority Queue), Bucket Sort, Counting

Acceptance Rate : 72.769 %


Solutions

Python

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)