Skip to content
BytePatterns

Reorganize String Gaps

MediumHeaps#heap#greedy~30m

Problem

Rearrange the letters of a string so that no two neighbours are the same letter. Return any arrangement that works, or the empty string when no arrangement exists.

Examples

Input:  s = "aab"
Output: "aba"
Why:    the two a's are separated by the b
Input:  s = "aaab"
Output: ""
Why:    three a's cannot be kept apart by a single b
Input:  s = "vvvlo"
Output: "vlvov"
Why:    the three v's are spaced out by the other two letters

Hints

0 / 3

Stuck on the idea rather than the code? Reorganize a String covers it.