Push Dominoes - Problem
Imagine you're looking at a line of n dominoes standing upright. Suddenly, some dominoes are pushed - some topple to the left (L), others to the right (R), while some remain upright (.).

Physics takes over! Each second, falling dominoes push their neighbors in the same direction. When a domino gets pushed from both sides with equal force, it stays perfectly balanced and upright.

Your mission: Given the initial state as a string where 'L' means pushed left, 'R' means pushed right, and '.' means upright, determine what the dominoes look like after they've all settled.

Example: "R.L" becomes "RRL" - the middle domino gets pushed right before the left force reaches it!

Input & Output

example_1.py — Basic Example
$ Input: dominoes = "RR.L"
Output: "RRRL"
💡 Note: The first domino is already falling right, the second is already falling right, the third gets pushed right by the second domino (force arrives at t=1) before being affected by the fourth domino's leftward force, and the fourth is already falling left.
example_2.py — Balanced Forces
$ Input: dominoes = ".L.R...LR..L.."
Output: "LL.RR.LLRRLL.."
💡 Note: Multiple force interactions create various regions where dominoes fall left, right, or remain upright when forces balance.
example_3.py — No Forces
$ Input: dominoes = "..."
Output: "..."
💡 Note: When no dominoes are initially pushed, they all remain upright.

Constraints

  • 1 ≤ dominoes.length ≤ 105
  • dominoes[i] is either 'L', 'R', or '.'
  • All dominoes fall at the same speed (1 position per second)

Visualization

Tap to expand
Push Dominoes - Optimal Solution INPUT Initial State: "RR.L" R [0] R [1] . [2] L [3] Forces Acting: R pushes right L pushes left Input String: "RR.L" ALGORITHM STEPS 1 Two Pointer Scan Track L and R forces 2 Calculate Distances Distance from each R and L 3 Compare Forces Closer force wins 4 Apply Result Equal = stays upright Processing "RR.L": pos[2] '.' analysis: R force: dist=1 (from pos 1) L force: dist=1 (from pos 3) 1 == 1 --> stays '.' Wait! L at pos 3 removed FINAL RESULT Final State: "RRR" R [0] R [1] R [2] Why "RRR"? - L at pos 3 falls left - Leaves boundary, removed - R forces push all right Output: "RRR" Key Insight: The problem is about force propagation. For each upright domino, calculate the nearest L (left force) and nearest R (right force). The closer force wins. Equal distances = stays upright. Use two-pass approach: one pass for R forces (left to right), one for L forces (right to left). O(n) time complexity. TutorialsPoint - Push Dominoes | Optimal Two-Pass Solution
Asked in
Facebook 45 Google 38 Microsoft 32 Amazon 28
89.2K Views
Medium Frequency
~18 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen