FastPrepTransform L/R Pieces on a Circular Board

Transform L/R Pieces on a Circular Board

Google logoGoogle● HardNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

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) → boolean

Examples

Example 1

start = ".L..R..R."target = "L......RR"circular = falsereturn = true

The 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 = true

The L piece leaves index 0 through the left boundary and re-enters at index 2.

Example 3

start = "RL.."target = "LR.."circular = truereturn = false

Each 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 '.'.

More Google problems

See Google hiring insights
public boolean canTransformLRPieces(String start, String target, boolean circular) {
    // write your code here
}
start".L..R..R."
target"L......RR"
circularfalse
expectedtrue
Checking account…