Loading...
Given a string s of lowercase English letters, rearrange its characters so that no two adjacent characters are equal.
Among all such rearrangements, return the lexicographically smallest one. If no valid rearrangement exists, return the empty string "".
A rearrangement uses every character of s exactly once.
Input: s = "aab"
Output: "aba"
Explanation: "aba" is the only rearrangement with no equal neighbors.
Input: s = "aaab"
Output: ""
Explanation: Three 'a's cannot be separated by a single 'b', so no valid rearrangement exists.
Input: s = "aabb"
Output: "abab"
Explanation: "abab" and "baba" both avoid equal neighbors; "abab" is lexicographically smaller.
s.length ≤500s consists of lowercase English letters.Click "Run" to test with sample cases or "Submit" to run all tests.