Time Needed to Rearrange a Binary String
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.
Follow up: Can you solve this problem in O(n) time complexity?
Example 1
Input
s = "0110101"Output
4After four seconds,
s becomes "1111000", with no occurrence of "01" remaining, so the process takes 4 seconds.Example 2
Input
s = "11100"Output
0No occurrence of
"01" exists in s, so the process takes 0 seconds.Constraints
- 1 <= s.length <= 1000
- s[i] is either '0' or '1'.