Zepruv › Coding problems

Swap Adjacent in LR String

Medium · Two Pointers, String

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.

Example 1

Input: start = "RXXLRXRXL", result = "XRLXXRRLX"
Output: true

We can transform start to result following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX

Example 2

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

Constraints

Starter code (python)

class Solution(object):
    def canTransform(self, start, result):
        """
        :type start: str
        :type result: str
        :rtype: bool
        """
        

Related problems

Previous: Binary Search · Next: K-th Symbol in Grammar · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.