FunctionRacerProblemsRace

Reachable by Sorting Pieces

HardStringsPython, JavaScript, TypeScript, Java, C++

start and goal are strings of digits with the same length. One move picks any piece of start (characters next to each other) and sorts that piece in rising order, in place.

Return True if some number of moves (zero moves allowed) turns start into goal, otherwise False.

Example 1

start "5231"
goal "2315"
returns true

Sort the piece 523 to get 2351, then sort the piece 51 to get 2315.

Example 2

start "132"
goal "312"
returns false

The 3 would have to move left past the 1. Sorting never moves a digit left past a smaller one.

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