Maximum Number of Operations to Move Ones to the End

You are given a binary string s.

You can perform the following operation on the string any number of times:

  • Choose any index i from the string where i + 1 < s.length such that s[i] == '1' and s[i + 1] == '0'.
  • Move the character s[i] to the right until it reaches the end of the string or another '1'.

Return the maximum number of operations that you can perform.

Example 1
Inputs = "1001101"
Output4
By repeatedly moving eligible 1 characters to the right, the maximum number of operations that can be performed is 4.
Example 2
Inputs = "00111"
Output0
There is no index i where s[i] == '1' and s[i + 1] == '0', so no operations can be performed.

Constraints

  • 1 <= s.length <= 10^5
  • s[i] is either '0' or '1'.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate