Sort Characters by Frequency

Given a string s, sort its characters in decreasing order of frequency and return the resulting string. Indexing is 0-based. Tie-break rule: if two characters…

Given a string **s**, sort its characters in **decreasing order of frequency** and return the resulting string. Indexing is **0-based**. **Tie-break rule:** if two characters have the same frequency, the one with the smaller ASCII code appears first (i.e. character ascending). This gives a unique canonical output required by this judge. **Examples:** - `"tree"` → `"eert"` (e appears twice; r < t by ASCII code) - `"cccaaa"` → `"aaaccc"` (both appear 3 times; a < c) - `"Aabb"` → `"bbAa"` (b appears twice; A (65) < a (97)) The canonical (frequency descending, ASCII code ascending) ordering is part of this problem's contract and gives one exact answer.

- `1 <= s.length <= 5 * 10^5` - `s` consists of uppercase and lowercase English letters and digits. - The interactive visualizer accepts `1 <= s.length <= 20`; longer valid judge inputs are execution-only. - Auxiliary space is measured without the required output string.

Example 1: input s = "tree"; output "eert". "e" appears 2 times, "r" and "t" appear 1 time each. Tie broken by ASCII code: r (114) < t (116).

Example 2: input s = "cccaaa"; output "aaaccc". "c" and "a" both appear 3 times. Tie broken by ASCII code: a (97) < c (99).

Example 3: input s = "Aabb"; output "bbAa". "b" appears 2 times; "A" and "a" appear 1 time each. Tie: A (65) < a (97).

Category: Strings. Topic: strings. Difficulty: Medium. Patterns: hashing.