Reachable by Sorting Pieces
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
1 <= len(start) <= 100len(goal) == len(start)startandgoalhold digits0to9
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Strings problems
- Scrambled by CutsHard
- Shortest Packing After RemovalsHard
- Smallest After Moving LettersHard
- Smallest Order Without TwinsHard
- Acronym of WordsBeginner
- Alternating CaseBeginner
- Capitalize the TitleBeginner
- Center in a FieldBeginner