FastPrepTransform L/R Pieces With Barriers

Transform L/R Pieces With Barriers

Google logoGoogle● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

start and target are boards of equal length containing four characters:

  • 'L' is a piece that moves only left.
  • 'R' is a piece that moves only right.
  • '.' is an empty cell.
  • '|' is an immovable barrier.

A piece may move through empty cells, but it may not pass through another piece or a barrier. Barriers never move. Pieces may move in any order.

Return whether start can be transformed into target.

Function

canTransformWithBarriers(start: String, target: String) → boolean

Examples

Example 1

start = ".L.|R.."target = "L..|..R"return = true

Within the left segment, L moves from index 1 to 0. Within the right segment, R moves from index 4 to 6. Neither piece crosses the barrier at index 3.

Example 2

start = "R..|..L"target = "..R|L.."return = true

R moves right inside the first segment and L moves left inside the second segment.

Example 3

start = "R.|..."target = "..|R.."return = false

The R piece would have to cross the fixed barrier at index 2.

Constraints

  • 1 <= start.length == target.length <= 100000.
  • Both strings contain only 'L', 'R', '.', and '|'.

More Google problems

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