Medium
You are given a binary string s.
You can perform the following operation on the string any number of times:
i from the string where i + 1 < s.length such that s[i] == '1' and s[i + 1] == '0'.s[i] to the right until it reaches the end of the string or another '1'. For example, for s = "010010", if we choose i = 1, the resulting string will be s = "0**001**10".Return the maximum number of operations that you can perform.
Example 1:
Input: s = “1001101”
Output: 4
Explanation:
We can perform the following operations:
i = 0. The resulting string is s = "**001**1101".i = 4. The resulting string is s = "0011**01**1".i = 3. The resulting string is s = "001**01**11".i = 2. The resulting string is s = "00**01**111".Example 2:
Input: s = “00111”
Output: 0
Constraints:
1 <= s.length <= 105s[i] is either '0' or '1'.public class Solution {
public int maxOperations(String s) {
char[] arr = s.toCharArray();
int result = 0;
int ones = 0;
int n = arr.length;
for (int i = 0; i < n; ++i) {
ones += arr[i] - '0';
if (i > 0 && arr[i] < arr[i - 1]) {
result += ones;
}
}
return result;
}
}