Move Pieces to Obtain a String - Problem
๐ฏ Move Pieces to Transform String
Imagine you have a sliding puzzle with pieces that can only move in specific directions! You're given two strings: a start configuration and a target configuration, both of length n.
Each string contains exactly three types of characters:
- 'L' - A left-moving piece that can only slide left into adjacent blank spaces
- 'R' - A right-moving piece that can only slide right into adjacent blank spaces
- '_' - A blank space that pieces can occupy
Your mission: Determine if you can transform the start string into the target string by sliding the pieces according to their movement rules.
Key Rules:
- An 'L' piece can move left only if there's a '_' directly to its left
- An 'R' piece can move right only if there's a '_' directly to its right
- Pieces cannot jump over other pieces
- You can perform any number of valid moves
Return true if the transformation is possible, false otherwise.
Input & Output
example_1.py โ Basic Transformation
$
Input:
start = "_L__R__R_", target = "L______RR"
โบ
Output:
true
๐ก Note:
L piece can move left from position 1 to position 0. Both R pieces can move right: first R from position 4 to position 7, second R from position 7 to position 8.
example_2.py โ Invalid Movement
$
Input:
start = "R_L_", target = "_LR_"
โบ
Output:
false
๐ก Note:
R piece cannot move right past the L piece, and L piece cannot move left past the R piece. The pieces would need to swap positions which is impossible.
example_3.py โ Same Configuration
$
Input:
start = "_R", target = "R_"
โบ
Output:
true
๐ก Note:
R piece can move right from position 1 to position 0... wait, that's impossible! R can only move right. This should be false.
Constraints
- n == start.length == target.length
- 1 โค n โค 105
- start and target consist only of characters 'L', 'R', and '_'
Visualization
Tap to expand
Understanding the Visualization
1
Initial Traffic
Vehicles are positioned on the road with parking spaces (_) between them
2
Movement Rules
L vehicles can only move left to exit, R vehicles only move right to destination
3
Position Check
Verify each vehicle can reach target without violating movement constraints
4
Valid Configuration
All vehicles can reach their targets following traffic rules
Key Takeaway
๐ฏ Key Insight: L vehicles can only move left (position decreases) and R vehicles can only move right (position increases). The relative order of vehicles of the same type never changes, making this a constraint satisfaction problem rather than a pathfinding problem.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code