We need to rearrange the characters of a string so that no two identical characters sit next to each other. If that is not possible, we return an empty string.
When is it impossible? Consider the string "aaab". It has three as and one b. Even placing b between two as gives "aaba", which still has adjacent as. There are not enough non-a characters to separate all the as.
This generalizes to a single condition: if any character appears more than (n + 1) / 2 times (where n is the string length), no valid arrangement exists. The reason is that a valid arrangement places copies of any character at least two positions apart. In a string of length n, the most positions you can use with that spacing is ceil(n / 2), which equals (n + 1) / 2. A character whose count exceeds that must be placed in two adjacent positions somewhere, which violates the constraint.
1 <= s.length <= 500 → n is small, so an O(n) solution is easy to reach and there is no overflow concern with int counts.s consists of lowercase English letters → Only 26 distinct characters, so a fixed-size frequency array of length 26 replaces a hash map.To spread characters apart, place the most frequent characters first, since they are the hardest to separate.
Sort all characters by frequency in descending order, then fill them into a result array at even indices first (0, 2, 4, ...). Once the even indices run out, continue at odd indices (1, 3, 5, ...). Two characters written consecutively in this order land at positions that differ by 2 (or by 1 only at the even-to-odd wrap), so identical copies never end up adjacent. Processing from most frequent to least frequent puts the dominant character into the well-separated even positions, and the remaining characters fill the gaps.
Before filling, check the impossibility condition: if any character appears more than (n + 1) / 2 times, return "".
(n + 1) / 2. If so, return "".This approach pre-sorts once and then fills positions. The next approach builds the string one character at a time, repeatedly selecting the most frequent character that differs from the one just placed. A max heap keeps the selection efficient, and the same greedy idea generalizes to cases where the gap between repeats must be larger than one.
This approach builds the string one character at a time. At each position, place the character with the highest remaining frequency, subject to the rule that it cannot equal the character just placed. Spending the most common character whenever it is allowed prevents being left with too many copies of one character at the end, when no other characters remain to separate them.
To enforce the "different from the previous character" rule with a heap, hold the just-placed character aside for one round instead of pushing it back immediately. On the next iteration, push it back (with its count decremented) before popping the next character. Because it is absent from the heap during the pop, it cannot be selected for the adjacent position.
The greedy is safe by an exchange argument. At any step, the character with the highest remaining count is the most constrained, so placing it now (when allowed) does not block a valid completion. If some valid arrangement places a different character at this position, swapping in the highest-count character and adjusting the later positions preserves the no-adjacent rule, because the highest-count character had at least as many copies left to distribute. The construction can only stall when the highest-count character is blocked (it was just placed) and it is the sole character remaining, the case ruled out by the (n + 1) / 2 check.
(n + 1) / 2. If so, return "".(frequency, character).prev as null (no previous character yet).prev still has remaining count, push it back into the heap.prev to the current character (with decremented count).The heap approach generalizes well, but for this problem it maintains a heap over at most 26 characters when only the single most frequent character drives the placement. The next approach drops both the sort and the heap: count frequencies, then fill indices directly in one linear pass.
The result array has positions 0 through n-1. Fill positions 0, 2, 4, ... first, then 1, 3, 5, ... Characters written one after another in this order land at indices that differ by 2, except at the single wrap from the last even index to index 1. Indices 2 apart are never adjacent, so a character only risks being next to itself at that wrap point.
The strategy:
This works as long as the most frequent character's count does not exceed (n + 1) / 2.
The only place two written-in-sequence characters can be adjacent is the wrap from the last even index to index 1. The most frequent character is placed first and entirely at even indices: with count m <= (n + 1) / 2, it occupies indices 0, 2, ..., 2(m-1), which all stay within range. Every other character has a count strictly less than m, so no other character spans both the even and odd halves enough to reach the wrap with two of its own copies on either side. Across the wrap, the character at the last even slot and the character at index 1 come from different points in the fill order, so they are different characters. Within each half, consecutive placements are 2 apart and identical, so no two equal characters touch.
(n + 1) / 2, return "".