516. Push Dominoes
A row of n dominoes stands on a table. At time zero, some of them are tipped: your function receives a string dominoes where dominoes[i] is 'L' if domino i was pushed toward the left, 'R' if it was pushed toward the right, and '.' if it is still standing.
Every second, each falling domino nudges the next standing neighbor on the side it is falling toward. A standing domino that is nudged from both sides at the same instant is perfectly balanced and stays upright. A domino that has already fallen never changes direction, and pushed dominoes exert no new force once they hit the table.
Return a string in the same format describing the row once nothing is moving anymore.
Example 1:
Input: dominoes = ".L.R...LR..L.."
Output: "LL.RR.LLRRLL.."
Explanation: The leading L drags index 0 down with it. Between the R at index 3 and the L at index 7 sit three dominoes: the outer two fall toward their pushers and the exact middle one (index 5) stays balanced. The trailing dominoes after the last L are past every force and never move.
Example 2:
Input: dominoes = "RR.L"
Output: "RR.L"
Explanation: The lone standing domino (index 2) is nudged from the left by the R and from the right by the L in the same second — balanced, so nothing changes.
Constraints:
- 1 ≤ dominoes.length ≤ 10⁵
- dominoes[i] is 'L', 'R', or '.'
Hints:
Direct simulation works: each second, compute every standing domino's fate from its two neighbors *simultaneously* (write into a fresh copy), and stop when a full pass changes nothing.
A standing domino's fate depends only on the nearest pushed domino to its left and to its right. Everything between two consecutive non-dot characters resolves independently of the rest of the row.
Add sentinels — an imaginary 'L' before the row and 'R' after it — then walk consecutive pairs of force characters: L…L fills with L, R…R with R, L…R stays dots, and R…L splits half and half with a possible balanced dot in the exact middle.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: dominoes = ".L.R...LR..L.."
Expected output: "LL.RR.LLRRLL.."