Swap Adjacent in LR String - Problem
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either:
- Replacing one occurrence of
"XL"with"LX", or - Replacing one occurrence of
"RX"with"XR"
Given the starting string start and the ending string result, return true if and only if there exists a sequence of moves to transform start to result.
Key insight: L can only move left, R can only move right, and their relative order must be preserved.
Input & Output
Example 1 — Basic Transformation
$
Input:
start = "RXXLRXRXL", result = "XRLXXRRLX"
›
Output:
true
💡 Note:
We can transform start to result: R can move right through X positions, L can move left through X positions. The sequence R,L,R,R,L is preserved in both strings.
Example 2 — Invalid Movement
$
Input:
start = "R", result = "L"
›
Output:
false
💡 Note:
Cannot change R to L - only movements allowed are XL→LX and RX→XR. Character types must remain the same.
Example 3 — Impossible Direction
$
Input:
start = "XXLR", result = "LRXX"
›
Output:
false
💡 Note:
L at position 2 cannot move right to position 0, and R at position 3 cannot move left to position 1. Movement constraints are violated.
Constraints
- 1 ≤ start.length, result.length ≤ 104
- start and result consist only of characters 'L', 'R', and 'X'
Visualization
Tap to expand
Understanding the Visualization
1
Input
Two strings with L, R, X characters
2
Allowed Moves
XL→LX (L moves left), RX→XR (R moves right)
3
Output
True if start can be transformed to result
Key Takeaway
🎯 Key Insight: L and R characters can only move in their designated directions through X positions, and their relative order must be preserved.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code