Skip to contentKEEP THE CRAFTTHE SANCTUARY

Sort Characters by Frequency

Rebuild a string so the most frequent characters come first, with each character's copies kept together.

The problem

Implement Solution.frequencySort(s). Return a rearrangement of s where all copies of each character sit next to each other and the groups are ordered from the most frequent character to the least frequent. Characters with the same frequency may come in either order. Uppercase letters, lowercase letters and digits are different characters. For example, "tree" can return "eetr" or "eert". 1 <= len(s) <= 500000.

Starter code

class Solution:
    def frequencySort(self, s):
        pass

Public examples

  1. The input described abovegivesTrue
  2. The input described abovegivesTrue
  3. The input described abovegivesTrue

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.