Push Dominoes

There are n dominoes in a line, and each domino is initially placed vertically upright. At the beginning, some dominoes are simultaneously pushed either to the left or to the right.

After each second:

  • Each domino falling to the left pushes the adjacent domino on its left.
  • Each domino falling to the right pushes the adjacent standing domino on its right.

When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.

For this problem, a falling domino expends no additional force on a domino that is falling or already fallen.

You are given a string dominoes representing the initial state:

  • dominoes[i] = 'L' if the i^th domino has been pushed to the left.
  • dominoes[i] = 'R' if the i^th domino has been pushed to the right.
  • dominoes[i] = '.' if the i^th domino has not been pushed.

Return a string representing the final state.

Example 1
Inputdominoes = "RR.L"
Output"RR.L"
The first domino expends no additional force on the second domino.
Example 2
Inputdominoes = ".L.R...LR..L.."
Output"LL.RR.LLRRLL.."
The pushes propagate through the standing dominoes to produce the final stable state.

Constraints

  • n == dominoes.length
  • 1 <= n <= 10^5
  • dominoes[i] is either 'L', 'R', or '.'.

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