Move Pieces to Obtain a String

You are given two strings start and target, both of length n. Each string consists only of the characters 'L', 'R', and '_' where:

  • The characters 'L' and 'R' represent pieces, where a piece 'L' can move to the left only if there is a blank space directly to its left, and a piece 'R' can move to the right only if there is a blank space directly to its right.
  • The character '_' represents a blank space that can be occupied by any of the 'L' or 'R' pieces.

Return true if it is possible to obtain the string target by moving the pieces of the string start any number of times. Otherwise, return false.

Example 1
Inputstart = "_L__R__R_", target = "L______RR"
Outputtrue
We can move the first piece left, the last piece right, and then the second piece three steps right to obtain target from start.
Example 2
Inputstart = "R_L_", target = "__LR"
Outputfalse
After moving the R piece one step right, no pieces can move anymore, so it is impossible to obtain target from start.

Constraints

  • n == start.length == target.length
  • 1 <= n <= 10^5
  • start and target consist of the characters 'L', 'R', and '_'

Asked at 5 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