Talentd
FleetCode

767. Reorganize String

Medium57.5% acceptance20 companies

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:

Input: s = "aab"
Output: "aba"

Example 2:

Input: s = "aaab"
Output: ""

Constraints:

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

Hints open one at a time

Hint 1
Alternate placing the most common letters.
12:44
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def reorganizeString(self, s: str) -> str:
3
Starter codePython 3Spaces: 4
Examples
Input
"aab"
Input
"aaab"
Your code is saved on this device.Solution