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
Swap Adjacent in LR String Single Pass Character Matching Approach INPUT start = "RXXLRXRXL" R X X L R X R X L result = "XRLXXRRLX" X R L X X R R L X Movement Rules: "RX" --> "XR" R moves RIGHT only "XL" --> "LX" L moves LEFT only Legend: R (Right mover) L (Left mover) X (Empty space) ALGORITHM STEPS 1 Check Length Both strings must have equal length (9 = 9) OK 2 Two Pointer Scan Skip X's, match L/R chars in same relative order 3 Position Check L in start must be at or after L in result 4 R Position Check R in start must be at or before R in result Character Matching: start: R L R R L | | | | | result: R L R R L idx: 0 3 4 5 7 1 2 5 6 7 (result) FINAL RESULT true Transformation Possible Verification: [OK] Same L,R sequence [OK] L positions valid: L@3 can move to 2 L@8 can move to 7 [OK] R positions valid: R@0 can move to 1 R@4,6 can reach 5,6 [OK] All checks passed! Sample Moves: RXXLRXRXL XRXLRXRXL (RX-->XR) XRLXRXRXL (XL-->LX) ... --> XRLXXRRLX Key Insight: L and R cannot pass through each other. L can only move LEFT (XL-->LX), R can only move RIGHT (RX-->XR). The relative order of L's and R's must remain unchanged. We only need to verify that each character can reach its target position: L's index in start >= result, R's index in start <= result. TutorialsPoint - Swap Adjacent in LR String | Single Pass Character Matching Approach Time Complexity: O(n) | Space Complexity: O(1)
Asked in
Google 45 Facebook 38 Microsoft 29 Amazon 22
28.5K Views
Medium Frequency
~25 min Avg. Time
891 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