Skip to the code
cf.
+25 XP

#598 Settle Falling Tiles

medium

A playful "domino" loading animation shows a row of tiles as a string row: "L" is a tile pushed to fall left, "R" a tile pushed to fall right, and "." a standing tile. Every second, each falling tile pushes the standing tile next to it in the same direction. A standing tile pushed from both sides at the same second stays standing. A fallen tile never pushes a tile that has already fallen.

Write settleTiles(row) that returns the row after every tile has stopped moving.

Example 1

Input: row = "RR.L"
Output: "RR.L"

The standing tile is pushed from both sides at the same moment, so it stays.

Example 2

Input: row = ".L.R...LR..L.."
Output: "LL.RR.LLRRLL.."

Each push spreads until it meets another push or a tile falling the other way.

Constraints

  • 0 <= row.length <= 100000
  • row contains only L, R, and ..
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).