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 end, return True if and only if there exists a sequence of moves to transform one string to the other.
Example:
Input: start = "RXXLRXRXL", end = "XRLXXRRLX"
Output: True
Explanation:
We can transform start to end following these steps:
RXXLRXRXL ->
XRXLRXRXL ->
XRLXRXRXL ->
XRLXXRRXL ->
XRLXXRRLX
Note:
- 1 <= len(start) = len(end) <= 10000.
- Both start and end will only consist of characters in {'L', 'R', 'X'}.
class Solution:
def canTransform(self, start, end):
"""
:type start: str
:type end: str
:rtype: bool
"""
if len(start)!=len(end):
return False
l = len(start)
i = j = 0
while i<l and j<l:
while j<l and end[j]=='X':
j += 1 #去掉X
while i<l and start[i]=='X':
i += 1 #去掉X
if i==l and j==l: #都走到最后
break
if i==l or j==l or end[j]!=start[i]: #只有一方走到最后、或者R对应上了L
return False
if start[i]=='L' and i<j: #L只能向左走
return False
if start[i]=='R' and i>j: #R只能向右走
return False
j += 1
i += 1
return True