MediumHeapAI interview only
Reorganize String
Asked atgoogleamazonmetamicrosoftbloombergappleuber
01 · Problem
Given a string s, rearrange the characters of s so that any two adjacent characters are not the same.
Return any valid rearrangement of s, or return "" if not possible.
02 · Examples
Example 01
Input
s = "aab"
Output
"aba"
We can rearrange "aab" to "aba" where no two adjacent characters are the same.
Example 02
Input
s = "aaab"
Output
""
It is not possible to rearrange the string so that no two adjacent characters are the same.
Example 03
Input
s = "aabb"
Output
"abab"
We can alternate the characters: "abab" or "baba" are both valid.
03 · Constraints
- 011 <= s.length <= 500
- 02s consists of lowercase English letters
04 · Optimal complexity
- Time
- O(n)
- Space
- O(1)
05 · Two ways to work on it
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.