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
Traffic Lane Movement PuzzleSingle Lane RoadInitial State: _L_R__๐Ÿ…ฟ๏ธ๐Ÿš‘ L๐Ÿ…ฟ๏ธ๐Ÿš› R๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธTarget State: L___R_๐Ÿš‘ L๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿš› R๐Ÿ…ฟ๏ธMovement Analysisโœ“ Emergency vehicle L: Position 1 โ†’ 0 (moved left โ†, valid)โœ“ Delivery truck R: Position 3 โ†’ 4 (moved right โ†’, valid)
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.
Asked in
Google 45 Facebook 32 Amazon 28 Microsoft 25
28.5K Views
Medium-High Frequency
~18 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen