Skip to content

Reorganize String

MediumHeap / Priority Queue
Decode it first. Clues, constraints, pattern and plan before any code.

Given a string s, rearrange its letters so that no two adjacent characters are the same. Return any valid arrangement, or "" if it is impossible.

"aab"   →  "aba"
"aaab"  →  ""      (three a's cannot be kept apart by one b)
"vvvlo" →  "vlvov" ("vovlv" is also fine)

Constraints

  • 1 <= s.length <= 500
  • s holds only lowercase English letters.