Swap Adjacent in LR 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
Inputstart = "RXXLRXRXL", result = "XRLXXRRLX"
Outputtrue
We can transform start to result by applying the allowed swaps in sequence: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX.
Example 2
Inputstart = "X", result = "L"
Outputfalse
There is no allowed move that can transform "X" into "L".

Constraints

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

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate