1. Brute Force (BFS)

class Solution:
    def canChange(self, start: str, target: str) -> bool:
        n = len(start)
        visit = {start}
        q = deque([start])

        while q:
            cur = q.popleft()
            if cur == target:
                return True
            for i in range(n - 1):
                pair = cur[i:i + 2]
                if pair == '_L' or pair == 'R_':
                    nxt = cur[:i] + pair[::-1] + cur[i + 2:]
                    if nxt not in visit:
                        visit.add(nxt)
                        q.append(nxt)
        return False

Time & Space Complexity

  • Time complexity: O(n2∗2n)O(n ^ 2 * 2 ^ n)
  • Space complexity: O(n∗2n)O(n * 2 ^ n)

Where nn is the length of the strings startstart and targettarget.


2. Queue

class Solution:
    def canChange(self, start: str, target: str) -> bool:
        n = len(start)
        startQ, targetQ = deque(), deque()
        for i in range(n):
            if start[i] != '_':
                startQ.append(i)
            if target[i] != '_':
                targetQ.append(i)

        if len(startQ) != len(targetQ):
            return False

        while startQ:
            i = startQ.popleft()
            j = targetQ.popleft()
            if start[i] != target[j]:
                return False
            if start[i] == 'L' and i < j:
                return False
            if start[i] == 'R' and i > j:
                return False
        return True

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(n)O(n)

Where nn is the length of the strings startstart and targettarget.


3. Prefix Count

class Solution:
    def canChange(self, start: str, target: str) -> bool:
        if start.replace('_', '') != target.replace('_', ''):
            return False

        l = r = 0
        for i in range(len(start)):
            if target[i] == 'L':
                l += 1
            if start[i] == 'L':
                l -= 1
            if start[i] == 'R':
                r += 1
            if target[i] == 'R':
                r -= 1
            if l < 0 or r < 0:
                return False
        return True

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(n)O(n)

Where nn is the length of the strings startstart and targettarget.


4. Two Pointers

class Solution:
    def canChange(self, start: str, target: str) -> bool:
        n = len(start)
        i = j = 0
        while i < n or j < n:
            while i < n and start[i] == '_':
                i += 1
            while j < n and target[j] == '_':
                j += 1

            if i == n or j == n:
                return i == n and j == n
            if start[i] != target[j]:
                return False
            if start[i] == 'L' and i < j:
                return False
            if start[i] == 'R' and i > j:
                return False
            i += 1
            j += 1
        return True

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1) extra space.

Where nn is the length of the strings startstart and targettarget.