Push Dominoes

A line of `n` dominoes stands upright. At the start some are pushed left (`"L"`), some are pushed right (`"R"`) and the rest are untouched (`"."`). Every second, each falling domino pushes its neighbour in the direction it is falling. A standing domino pushed from both sides at the same time stays upright, and a falling domino adds no extra force to one that is already falling. Return the final state of the line as a string.

30:00

Push Dominoes
medium
Free written solution
Topics
Companies

A line of n dominoes stands upright. At the start some are pushed left ("L"), some are pushed right ("R") and the rest are untouched ("."). Every second, each falling domino pushes its neighbour in the direction it is falling. A standing domino pushed from both sides at the same time stays upright, and a falling domino adds no extra force to one that is already falling. Return the final state of the line as a string.

Example 1:
Input: {"dominoes":"RR.L"}
Output: RR.L
Constraints:
  • n==dominoes.lengthn == \text{dominoes.length}

  • 1≤n≤1051 \leq n \leq 10^5

  • dominoes[i]\text{dominoes}[i] is 'L', 'R' or '.'

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"dominoes":"RR.L"}
R…R / L…L: all fall that way
R…L: fall inward, odd middle stays
L…R: nothing moves
▌ falls right▌ falls left▌ standingdashed = virtual walls

result so far

RR.L

Pad the line with a virtual L wall on the left and a virtual R wall on the right. They lean away from the line, so they never push anything, but they give every gap a pushed domino on both sides.

Variables
VariableValue
n4
segments4
DepthFunction Call
Stack empty
0/6