15% off all plans & roadmaps Use code
See plansLeetCode 767 · Solution & editorial
Given a string s, rearrange the characters of s so that any two adjacent characters are not the same.
Reorganize String is a Heap (Priority Queue) Patterns problem, in the Scheduling / Minimum Cost (Greedy with Priority Queue) subpattern. The pattern covers top K elements, K-way merge, two heaps for median, and scheduling
Solve scheduling and optimization problems using priority queue with greedy approach.
The reference solution runs in O(nloga) time and O(a) space.
Reorganize String has been reported in interviews at Amazon, Google, Facebook, Microsoft, Wish, Uber, Goldman Sachs.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Reorganize String is a Heap (Priority Queue) Patterns problem, in the Scheduling / Minimum Cost (Greedy with Priority Queue) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.