Reorganize String

Given a string s, rearrange the characters of s so that any two adjacent characters are not the same.

Return any possible rearrangement of s or return "" if not possible.

Example 1
Inputs = "aab"
Output"aba"
The characters can be rearranged as "aba", where no two adjacent characters are the same.
Example 2
Inputs = "aaab"
Output""
There is no way to rearrange "aaab" so that no two adjacent characters are the same.

Constraints

  • 1 <= s.length <= 500
  • s consists of lowercase English letters.

Asked at 21 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate