# LeetCode: 777. 在LR字符串中交换相邻字符¶

## 1、题目描述¶

输入: start = "RXXLRXRXL", end = "XRLXXRRLX"

RXXLRXRXL ->
XRXLRXRXL ->
XRLXRXRXL ->
XRLXXRRXL ->
XRLXXRRLX


• $1 <= len(start) = len(end) <= 10000。$
• start和end中的字符串仅限于'L', 'R'和'X'。

## 2、解题思路¶

• "X"看成是通路，"L"只能向左走，"R"只能向右走

class Solution:
def canTransform(self, start: str, end: str) -> bool:
if len(start) != len(end):
return False
start_pos, end_pos = 0, 0

while start_pos < len(start) and end_pos < len(end):
while start_pos < len(start) and start[start_pos] == "X":
start_pos += 1
while end_pos < len(end) and end[end_pos] == "X":
end_pos += 1

if start_pos >= len(start) and end_pos >= len(end):
break
elif start_pos >= len(start) and end_pos < len(end):
return False
elif start_pos < len(start) and end_pos >= len(end):
return False

if start[start_pos] != end[end_pos]:
return False

if start[start_pos] == "R":
if start_pos > end_pos:
return False
if start[start_pos] == "L":
if start_pos < end_pos:
return False
start_pos += 1
end_pos += 1
return True