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.