Skip to content

Practice Time Needed to Rearrange a Binary String

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2380
LeetCode Problem

Time Needed to Rearrange a Binary String

Problem Statement

You are given a binary string s. In one second, all occurrences of "01" are simultaneously replaced with "10". This process repeats until no occurrences of "01" exist. Return the number of seconds needed to complete this process. Example 1: Example 2: Constraints: Follow up: Can you solve this problem in O(n) time complexity?

Source: Time Needed to Rearrange a Binary String on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "0110101"
Output
4

# Example 2

Input
s = "11100"
Output
0

Constraints

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

Related LeetCode Problems

Practice more problems similar to Time Needed to Rearrange a Binary String.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.