Transform L/R Pieces on a Circular Board
Problem statement
start and target describe boards of equal length. Each cell is 'L', 'R', or '.'.
- An
'L'piece moves only left. - An
'R'piece moves only right. - A piece may move any positive distance through empty cells, but it may not pass through or land on another piece.
- Pieces may move in any order.
If circular is false, pieces cannot leave the board. If circular is true, the board wraps: moving left from index 0 continues at index n - 1, and moving right from index n - 1 continues at index 0.
Return whether start can be transformed into target.
Function
canTransformLRPieces(start: String, target: String, circular: boolean) → booleanExamples
Example 1
start = ".L..R..R."target = "L......RR"circular = falsereturn = trueThe nonempty piece order is unchanged. L moves from 1 to 0, while the two R pieces move from 4 and 7 to 7 and 8.
Example 2
start = "L.."target = "..L"circular = truereturn = trueThe L piece leaves index 0 through the left boundary and re-enters at index 2.
Example 3
start = "RL.."target = "LR.."circular = truereturn = falseEach piece is immediately blocked in its allowed direction. Reaching the target would require the two pieces to cross.
Constraints
1 <= start.length == target.length <= 3000.- Both strings contain only
'L','R', and'.'.