2Brute2Best
🔥0

Sort Characters By Frequency

Medium·LeetCode #451 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You get a string s. Reorder its characters so that the most frequent character comes first, then the next most frequent, and so on. All copies of one character must sit next to each other.

If two characters appear the same number of times, either one may come first. Return the new string.

Input

A string s.

Output

A string with the same characters, where each character's copies are together and characters with more copies come first.

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

e appears twice, so it comes first. r and t appear once each, so they may come in either order. eetr is also accepted.

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

c and a both appear three times, so either can come first. cacaca is wrong, because all copies of a character must be together.

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

A and a are different characters, and each appears once. b appears twice, so it must come first. Aabb is wrong for the same reason.

Constraints
  • • 1 ≤ s.length ≤ 5 · 10^5
  • • `s` consists of uppercase letters, lowercase letters and digits only.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“sort by frequency”
→ Count first. The key is the count, not the character.
“decreasing order”
→ Highest count first: a bucket walk from n down, or a max-heap.
“same characters together”
→ Each character is one block of copies, written in one go.
“return any of them”
→ Ties may come in any order, so check the rules, not one exact string.
03

Approach Complexity Ladder

Brute Force — count, then sort the charactersO(N log N)O(1)

Count the characters, then sort the string by (count high to low, character low to high)

Better — max-heap of (count, char), write all copiesO(N + D log D)O(N)

Count the characters; pop from a max-heap of (count, char) and write all copies of each

Optimal — bucket the characters by count, walk downO(N)O(N)

Count the characters; bucket them by count; walk from n down, writing each block

Brute Force Intuition

Count each character. Then sort the string itself with a comparator: a character with more copies goes first. When the counts are equal, compare the characters, so copies of one letter stay together.

Recognition rule for this sub-pattern:

Count first, then keep the K most frequent in a heap (or bucket by count).
Remember this · Heap — Top K
Trigger
"K largest / smallest / most frequent / closest", kth element, streaming top-K.
Think
Do I really need everything sorted, or only the best K?
Key question
Min-heap or max-heap of size K — which one lets me kick out the worst candidate?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Heap — Top K translate into production architectures and low-level memory mechanics.