↓Skip to main content
  1. LeetCode/

LeetCode 777: Swap Adjacent in LR String

·2 mins· ·
LeetCode Medium Two-Pointers String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-02-01

  • Submissions: Wrong Answer ×2 → Accepted

Approach
#

Translate the moves:

  • "XL" → "LX": L moves one step left.
  • "RX" → "XR": R moves one step right.

X is a blank, and L and R cannot pass each other, so this is exactly LeetCode 2337:

  1. With X removed, start and result must have the same sequence.
  2. Each L’s index in start must be >= its index in result; each R’s must be <=.

My Attempts
#

  1. Attempt 1 only compared Counter(start) == Counter(result). For start = "LR", result = "RL", the counts match but L and R cannot pass each other, so the answer is False; this version returned True.
  2. Attempt 2 used two pointers to skip X and compare order, but did not check direction. For start = "LX", result = "XL", the order matches but L would have to move right, so the answer is False; this version returned True.

Adding the direction checks passed.

Solution
#

class Solution:
    def canTransform(self, start: str, result: str) -> bool:
        n = len(start)
        i = j = 0
        while True:
            while i < n and start[i] == "X":
                i += 1
            while j < n and result[j] == "X":
                j += 1
            if i == n or j == n:
                return i == n and j == n
            if start[i] != result[j]:
                return False
            if start[i] == "L" and i < j:
                return False                      # L only moves left
            if start[i] == "R" and i > j:
                return False                      # R only moves right
            i += 1
            j += 1

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Comparing character counts only.
  • Comparing order only and forgetting that L only moves left and R only moves right.

Interview Explanation
#

Replacing XL with LX moves L left, and replacing RX with XR moves R right; X is a blank and pieces cannot pass each other. Two pointers skip X and pair the k-th pieces: same type, an L in start not left of its spot in result, an R not right of it, and both sides run out together. Time O(n).

Related

LeetCode 2337: Move Pieces to Obtain a String
·2 mins
LeetCode Medium Two-Pointers String
LeetCode 2337 note: after skipping blanks the piece order must match; L only moves left and R only moves right, so each L’s start index must be >= its target index and each R’s must be <=
LeetCode 809: Expressive Words
·3 mins
LeetCode Medium Two-Pointers String Array
LeetCode 809 note: two pointers compare runs of equal characters; when the lengths differ, the run in s must be at least 3 and no shorter than the word’s run, and both strings must end together
LeetCode 1616: Split Two Strings to Make Palindrome
·3 mins
LeetCode Medium Two-Pointers String Greedy
LeetCode 1616 note: greedily match a’s prefix against b’s suffix from the outside in; the leftover middle must be a palindrome entirely within a or b, and both directions need checking
LeetCode 1750: Minimum Length of String After Deleting Similar Ends
·3 mins
LeetCode Medium Two-Pointers String
LeetCode 1750 note: when both ends match, each side eats its whole run of that character; a string of one repeated character can be deleted entirely
LeetCode 151: Reverse Words in a String
·1 min
LeetCode Medium Two-Pointers String
LeetCode 151 note: split() with no argument treats runs of spaces as one separator and drops leading and trailing spaces; reverse and join with single spaces
LeetCode 647: Palindromic Substrings
·1 min
LeetCode Medium Two-Pointers String Dynamic-Programming
LeetCode 647 note: expand around centers, counting one palindromic substring for each successful expansion