Smallest Order Without Twins
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
1 <= len(text) <= 60textholds lowercase letters
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Strings problems
- Acronym of WordsBeginner
- Alternating CaseBeginner
- Capitalize the TitleBeginner
- Center in a FieldBeginner
- Chunks From the LeftBeginner
- Consecutive CharactersBeginner
- Count Capitalized WordsBeginner
- Count Differing PositionsBeginner