All problems
0621MediumStringDynamic ProgrammingSimulation

Beads Sliding Left

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2380Time Needed to Rearrange a Binary String

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A groove holds a row of tiles described by s, read from left to right. The character '1' is a bead and '0' is an empty tile.

Every second, all beads move at the same instant: a bead whose immediate left neighbour is an empty tile slides into that tile, and every other bead stays put. Since the moves are simultaneous, whether a bead slides is decided by the layout at the start of the second, not by what its neighbours do during it.

Return the number of seconds after which no bead has an empty tile immediately to its left. If the groove already looks like that, return 0.

Examples

Example 1

Input
s = "0011"
Output
3

The groove goes `0011` -> `0101` -> `1010` -> `1100`. After the third second no bead has an empty tile on its left.

Example 2

Input
s = "1010"
Output
1

The groove goes `1010` -> `1100`, so one second is enough: the bead at position 3 slides into position 2 while the bead at position 0 has no empty tile on its left.

Example 3

Input
s = "1111"
Output
0

There is no empty tile at all, so no bead can ever slide.

Constraints

  • 1 <= s.length <= 1000
  • s[i] is either '0' or '1'.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def seconds_to_remove_occurrences(s: str) -> int:
Java
public int secondsToRemoveOccurrences(String s)
September 7
Apply