Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Heap / Priority Queue

Reorganize String

MEDIUM✓ CHIP-TIMEDLC #767 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: Reshuffle a string's letters so no two identical letters end up next to each other — return one valid arrangement if any exists, or empty if the letters are too lopsided to ever separate.

A string of lowercase letters arrives, and the goal is rearranging its own letters — not choosing new ones — so that no two identical letters end up adjacent.

Some inputs are simply too lopsided to ever separate, when one letter alone makes up more than half the string; in that case the expected answer is an empty result signaling impossibility.

Any valid rearrangement is accepted when one exists — there's no single correct output, just a check that no two neighbors in the returned string match.

  • string lengths run up to a few thousand characters
  • only lowercase letters appear in the input
  • impossible inputs expect an empty string back
  • any valid no-adjacent-repeat arrangement is accepted
EX 01
s = "aa"
→ [""]
two identical letters, impossible
EX 02
s = "aaab"
→ [""]
one letter is over half the string, impossible
EX 03
s = "a"
→ ["a"]
minimum size, trivially valid

The hintstake only what you need

HINT 1 THE NUDGE

A letter that shows up more than half the string (rounding up) can never be fully separated from itself — check that ceiling first before trying to build anything.

HINT 2 THE STRUCTURE

Placing the single most frequent remaining letter at every step, as long as it's not the letter you just placed, keeps every other letter's options open the longest.

HINT 3 ONE STEP FROM THE ANSWER

Keep letter counts in a max-heap. Pop the most frequent, place it, and if it's the same letter you just placed, temporarily pop the second-most-frequent instead — then push whatever still has count left back in.

Coach's boardthe pattern, step by step

THE MAX-HEAP SHUFFLEPATTERN · MAX-HEAP OF LETTER COUNTSs = "aaabbc" — counts a=3, b=2, c=1
STEP 1

Counts start a=3, b=2, c=1 over 6 letters. The heaviest letter, 3, isn't over half of 6, so a valid arrangement exists.

STEP 1 / 8 · ← → WORK TOO

The splitstwo paces, two languages

grind/reorganize-string.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def reorganizeString(self, s: str) -> str:
        counts = collections.Counter(s)
        n = len(s)
        if counts and max(counts.values()) > (n + 1) // 2:
            return ""

        heap = [(-c, ch) for ch, c in counts.items()]
        heapq.heapify(heap)
        result = []
        while heap:
            c1, ch1 = heapq.heappop(heap)
            if result and result[-1] == ch1:
                if not heap:
                    return ""
                c2, ch2 = heapq.heappop(heap)
                result.append(ch2)
                c2 += 1
                if c2 < 0:
                    heapq.heappush(heap, (c2, ch2))
                heapq.heappush(heap, (c1, ch1))
            else:
                result.append(ch1)
                c1 += 1
                if c1 < 0:
                    heapq.heappush(heap, (c1, ch1))
        return "".join(result)
TIME O(N LOG 26)SPACE O(26)PYTHON · RACE PACE · 27 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED