889. No Two Neighbours the Same
Rearrange the letters of a lower-case word so that no two ADJACENT letters are equal, or report that it cannot be done.
Several rearrangements can be valid, so the answer is pinned down by a rule: at each position place the letter with the LARGEST remaining count among the letters that are not equal to the letter just placed, and break a tie between equal counts by choosing the alphabetically smaller letter.
If no arrangement exists, print impossible.
aab -> aba
aabb -> abab
aaab -> impossible
abcabc -> abcabc
Trace aabb. Counts are a:2, b:2. Both are largest, so a wins the tie.
Now a:1, b:2, and a is banned as the previous letter, so b goes next.
Now a:1, b:1, b is banned, so a. Then b. The result is abab.
## When it is impossible
Let f be the largest count and n the length. The copies of that letter
must be separated by at least one other letter each, so they need f - 1
separators, and the word has n - f other letters. An arrangement exists
exactly when
f - 1 <= n - f which rearranges to f <= (n + 1) / 2
with integer division. aaab has f = 3 and n = 4, and
(4 + 1) / 2 = 2, so it fails. aab has f = 2 and (3 + 1) / 2 = 2, so
it just fits.
## What most people try first
Sorting the letters and dealing them out. aabb sorted is aabb, and
dealing left to right gives aabb, which is invalid. Dealing into alternate
positions does work for some inputs and is fiddly to get right around the tie
rule.
Scanning all 26 counters at every position. This is correct and is the
brute force these tests were checked against. It costs 26n, which at
n = 200000 is 5.2 * 10^6 and is fine here, but it stops being fine the
moment the alphabet is large, and the point of the exercise is the structure
rather than the alphabet.
## The greedy, and why the largest count
Placing the most common remaining letter first is the right move because that
letter is the one most in danger of being stranded. Formally: if any valid
completion exists, one exists that starts with a letter of maximum remaining
count. Suppose a valid completion starts with letter y while x has a
strictly larger remaining count. Somewhere later the arrangement places x;
swapping the first occurrence of x with this y keeps the arrangement
valid, because x was already legal at the swapped position and y is at
least as easy to place as x at the front. So placing the maximum first never
loses.
## The heap, and the one-step delay
The letter just placed is banned for exactly one position and then legal again. So it cannot sit in the heap during the next choice, and it must be back for the one after.
h = max-heap of (remaining count, letter), ties by the smaller letter
held = nothing
while h is not empty:
(count, letter) = pop h
append letter
if held is not nothing: push held, held = nothing
if count - 1 > 0: held = (count - 1, letter)
The letter sits in held for exactly one round, which is precisely the
cooldown the rule describes.
## The tie rule, spelled out
The heap key is (-count, letter) under a min-heap, or equivalently a
max-heap in which a larger count wins and, on equal counts, the smaller letter
wins. Without the second half, aabbccdd has four letters tied at 2 for the
first position and the output depends on the library's heap internals.
## The boundary that breaks a careless implementation
Checking feasibility before building. If f > (n + 1) / 2 the greedy
runs out of legal letters partway and either crashes or emits a repeat. Test
the inequality first and print impossible.
A single letter. a is valid, since there is no adjacent pair at all, and
(1 + 1) / 2 = 1 which is at least f = 1. aa is not, since
(2 + 1) / 2 = 1 is less than 2.
## Related problems
Problem 871 has the same greedy shape with a cooldown of c instead of 1, and
it asks for the total TIME rather than the arrangement, which is why it has a
closed-form answer and this does not. Problem 311 asks a different question
about letter counts entirely: the longest palindrome the letters can build.
## Cost
Time O(n log A) where A is the alphabet size, at most 26. Space O(n)
for the answer.
Constraints - The word has between 1 and 200000 characters. - Lower-case English letters only.
Input
A single line containing the word, made of lower-case English letters only.
Output
Print the rearranged word on one line, or the single word impossible if no arrangement with distinct adjacent letters exists. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.