FunctionRacerProblemsRace

Smallest Order Without Twins

HardStringsPython, JavaScript, TypeScript, Java, C++

Put the letters of text in a new order in which no two letters next to each other are the same. Return the alphabetically smallest such order, or an empty string if there is none.

Example 1

text "aabbb"
returns "babab"

Three b's and two a's fit in one way only: the b's take every other place.

Example 2

text "banana"
returns "abanan"

Each place takes the smallest letter that differs from the one before it and still leaves the rest placeable.

Example 3

text "aaab"
returns ""

Three a's cannot be kept apart with a single other letter.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Strings problems