Rearrange a string by decreasing character frequency

Quick Overview

Rearrange a string so its characters appear in decreasing order of how often they occur, with ties broken by character code. It tests frequency counting with a hash map, custom sort keys, and building a long output string efficiently.

Rearrange a string by decreasing character frequency

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `s`, rearrange its characters in decreasing order of frequency. The frequency of a character is the number of times it appears in `s`. Return the rearranged string. ### Function Signature ```python def frequency_sort(s: str) -> str: ``` ### Rules - The result uses every character of `s` exactly as many times as it appears in `s`. - All copies of a character sit next to each other, and characters with a higher frequency come first. - Uppercase and lowercase letters are different characters, so `'A'` and `'a'` are counted separately. - When two characters have the same frequency, the one with the smaller character code comes first. In ASCII order, digits come before uppercase letters and uppercase letters come before lowercase letters. This rule makes the expected output unique. ### Constraints - `1 <= len(s) <= 5 * 10^5` - `s` consists of uppercase and lowercase English letters and digits. ### Examples ```text Input: s = "tree" Output: "eert" ``` `'e'` appears twice, while `'r'` and `'t'` appear once each. The tie between `'r'` and `'t'` goes to `'r'`, which has the smaller character code. ```text Input: s = "cccaaa" Output: "aaaccc" ``` Both `'a'` and `'c'` appear three times, so the tie rule puts `'a'` first. A result such as `"cacaca"` would be wrong because the copies of each character must be contiguous. ```text Input: s = "Aabb" Output: "bbAa" ``` `'b'` appears twice and comes first. `'A'` and `'a'` are different characters with one occurrence each, and `'A'` (code 65) sorts before `'a'` (code 97).

Overview: Rearrange a string so its characters appear in decreasing order of how often they occur, with ties broken by character code. It tests frequency counting with a hash map, custom sort keys, and building a long output string efficiently.

General logo
General
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string s, rearrange its characters in decreasing order of frequency. The frequency of a character is the number of times it appears in s. Return the rearranged string.

Function Signature

def frequency_sort(s: str) -> str:

Rules

  • The result uses every character of s exactly as many times as it appears in s .
  • All copies of a character sit next to each other, and characters with a higher frequency come first.
  • Uppercase and lowercase letters are different characters, so 'A' and 'a' are counted separately.
  • When two characters have the same frequency, the one with the smaller character code comes first. In ASCII order, digits come before uppercase letters and uppercase letters come before lowercase letters. This rule makes the expected output unique.

Constraints

  • 1 <= len(s) <= 5 * 10^5
  • s consists of uppercase and lowercase English letters and digits.

Examples

Input: s = "tree"
Output: "eert"

'e' appears twice, while 'r' and 't' appear once each. The tie between 'r' and 't' goes to 'r', which has the smaller character code.

Input: s = "cccaaa"
Output: "aaaccc"

Both 'a' and 'c' appear three times, so the tie rule puts 'a' first. A result such as "cacaca" would be wrong because the copies of each character must be contiguous.

Input: s = "Aabb"
Output: "bbAa"

'b' appears twice and comes first. 'A' and 'a' are different characters with one occurrence each, and 'A' (code 65) sorts before 'a' (code 97).