Maximum Number of Operations to Move Ones to the End
You receive a binary string s .
You can perform the following operation on the text any number of times:
-
Choose any index
ifrom the text wherei + 1 < s.lengthsuch thats[i] == '1'ands[i + 1] == '0'. -
Move the character
s[i]to the right until it reaches the end of the text or another'1'. For example, fors = "010010", if we choosei = 1, the resulting string will bes = "000110".
Produce the maximum number of operations that you can perform.
Input: s = "1001101"
Output: 4
Explanation: We can perform the following operations:
-
Choose index
i = 0. The resulting string iss = "**001**1101". -
Choose index
i = 4. The resulting string iss = "0011**01**1". -
Choose index
i = 3. The resulting string iss = "001**01**11". -
Choose index
i = 2. The resulting string iss = "00**01**111".
Input: s = "00111"
Output: 0
Constraints
1 <= s.length <= 10^5s[i]is either'0'or'1'.
Did this problem appear in one of your interviews?
Responses stay private.
