> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Swap Adjacent in LR String Python Solution

> Tested Python solution for LeetCode 777 with 31 pytest cases. Generate a practice environment with lcpy.

LeetCode 777, [Medium](/catalog/medium). Topics: [Two Pointers](/catalog/topics/two-pointers), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/swap-adjacent-in-lr-string/description/).

Generate this problem as a practice environment: tested reference solution, 31 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 777   # by problem number
lcpy gen -s swap_adjacent_in_lr_string   # by problem name
```

## 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`.

### Examples

```
Input: start = "RXXLRXRXL", result = "XRLXXRRLX"
Output: true
Explanation: We can transform start to result following these steps:
RXXLRXRXL ->
XRXLRXRXL ->
XRLXRXRXL ->
XRLXXRRXL ->
XRLXXRRLX
```

```
Input: start = "X", result = "L"
Output: false
```

### Constraints

* `1 <= start.length <= 10^4`
* `start.length == result.length`
* Both `start` and `result` will only consist of characters in `'L'`, `'R'`, and `'X'`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/swap_adjacent_in_lr_string/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/swap_adjacent_in_lr_string/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def can_transform(self, start: str, result: str) -> bool:
        if len(start) != len(result):
            return False
        if start.replace("X", "") != result.replace("X", ""):
            return False
        i = 0
        j = 0
        n = len(start)
        while i < n and j < n:
            while i < n and start[i] == "X":
                i += 1
            while j < n and result[j] == "X":
                j += 1
            if i == n or j == n:
                break
            if start[i] != result[j]:
                return False
            # 'L' can only move left, 'R' can only move right
            if (start[i] == "L" and i < j) or (start[i] == "R" and i > j):
                return False
            i += 1
            j += 1
        return True
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.