Check if an L/R/X string can reach a target by moving L left and R right

Quick Overview

Given two equal-length strings of L, R and X characters, decide whether moves that replace XL with LX and RX with XR can turn the first string into the second. It tests finding the invariants of a move system and checking them in one linear scan.

Check if an L/R/X string can reach a target by moving L left and R right

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given two strings, `start` and `end`, of the same length. Each string consists only of the characters `'L'`, `'R'` and `'X'`, for example `"RXXLRXRXL"`. A move consists of either replacing one occurrence of `"XL"` with `"LX"`, or replacing one occurrence of `"RX"` with `"XR"`. Return `True` if and only if there exists a sequence of moves that transforms `start` into `end`. ### Function Signature ```python def can_transform(start: str, end: str) -> bool: ``` ### Rules - Moves are applied one at a time, and any number of moves is allowed, including zero. If `start == end`, the answer is `True`. - Only the two replacements above are allowed. `"LX"` cannot be turned back into `"XL"`, and `"XR"` cannot be turned back into `"RX"`. ### Constraints - `1 <= len(start) <= 10^4` - `len(start) == len(end)` - Both `start` and `end` consist only of the characters `'L'`, `'R'` and `'X'`. ### Examples ```text Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True ``` One valid sequence is `RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX`, which uses four moves. ```text Input: start = "X", end = "L" Output: False ``` There is no `L` or `R` in `start` that could produce the `L` in `end`, so the transformation is impossible. ```text Input: start = "RXLX", end = "XRXL" Output: False ``` The `R` can move one step right, but the `L` would have to move from index 2 to index 3, and a move only ever shifts an `L` to the left.

Overview: Given two equal-length strings of L, R and X characters, decide whether moves that replace XL with LX and RX with XR can turn the first string into the second. It tests finding the invariants of a move system and checking them in one linear scan.

ByteDance logo
ByteDance
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given two strings, start and end, of the same length. Each string consists only of the characters 'L', 'R' and 'X', for example "RXXLRXRXL". A move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR".

Return True if and only if there exists a sequence of moves that transforms start into end.

Function Signature

def can_transform(start: str, end: str) -> bool:

Rules

  • Moves are applied one at a time, and any number of moves is allowed, including zero. If start == end , the answer is True .
  • Only the two replacements above are allowed. "LX" cannot be turned back into "XL" , and "XR" cannot be turned back into "RX" .

Constraints

  • 1 <= len(start) <= 10^4
  • len(start) == len(end)
  • Both start and end consist only of the characters 'L' , 'R' and 'X' .

Examples

Input: start = "RXXLRXRXL", end = "XRLXXRRLX"
Output: True

One valid sequence is RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX, which uses four moves.

Input: start = "X", end = "L"
Output: False

There is no L or R in start that could produce the L in end, so the transformation is impossible.

Input: start = "RXLX", end = "XRXL"
Output: False

The R can move one step right, but the L would have to move from index 2 to index 3, and a move only ever shifts an L to the left.