Skip to content
Decoder

Reorganize String

Medium

Problem

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.

Example 1

Input
"aab"
Output
"aba"

Example 2

Input
"aaab"
Output
""
Why
three a's cannot be kept apart by one b

Example 3

Input
"vvvlo"
Output
"vlvov"
Why
"vovlv" is also fine

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.