Reorganize String
Check the most frequent letter first: if it appears more than (n + 1) / 2 times, return "". Otherwise keep a max-heap of (count, letter). Each round, pop the top two letters, write both, and push each back with one copy fewer. A leftover letter, if any, goes at the end.
Two neighbours must differ, and the most frequent letter can't be more than (n + 1) / 2. That second check is the only way the answer can be impossible, so it runs before anything else.
Problem
You get a string s. Rearrange its letters so that no two neighbours are the same letter.
Return any arrangement that works. If none exists, return the empty string "".
A string s of lowercase English letters.
A rearrangement of s with no two equal letters next to each other, or "" if that is impossible.
aab and baa both have two a's side by side. Only aba works.
Three a's need a letter between each pair, so they need two separators. There is only one b.
The answer is not unique. Every a sits between two non-a letters.
- • 1 ≤ s.length ≤ 500
- • `s` contains only lowercase English letters.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
Which letter is the hardest to keep apart from itself? Does its count matter more than the others?
- 2.
A string of length 5 with three copies of one letter: can you space them out? What about length 4?
- 3.
When you place a letter, what is the only thing that decides whether the next letter is allowed?
- 4.
If you always place the letter with the most copies left, does that help or hurt you later?
- 5.
Could you place two letters at a time instead of one, and would that avoid some trouble?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Try the arrangements of the letters in order; return the first with no equal neighbours | O(N · N!) | O(N) |
| Optimal | Max-heap of letter counts; each round writes the top two letters and holds them back with one copy less | O(N log 26) | O(N) |
Why does this belong to this pattern?
- “no two adjacent characters are the same”Every copy of a letter needs a different letter after it. Each letter needs room.
- “the most frequent letter”The letter with the most copies runs out of room first, so its count decides whether an answer exists at all.
- “return any valid answer, or the empty string”One count gives the yes or no. The rest of the work builds one answer.
This is the anchor of the heap-assisted sub-pattern. The best letter to place next changes after every round, because each placement lowers a count. A max-heap keeps the current best letter on top. The twist is that we place two letters per round, not one. Placing one at a time lets the same letter stay on top and end up next to itself (see the mistakes). Task Scheduler keeps a heap too, but it adds a cooldown; Hand of Straights uses a sorted map instead.
Don't make this mistake
This is the layout for Sort Characters by Frequency, where neighbours don't matter. Here it fails at once: aaabbc gives aaabbc, and the three a's touch.
After one copy of a is written, a still has the most copies, so it is on top again and gets written again. On aaabc the output starts aacba, with two a's side by side. Holding back both letters of a round is what stops this.
For aba (n = 3, a appears 2 times), n / 2 = 1, so the wrong check returns "". But aba is a valid answer. The most frequent letter can take every other slot starting from the first, so (n + 1) / 2 is the real limit.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.