Maximum Number of Operations to Move Ones to the End
MediumUpdated: Aug 2, 2025
Practice on:
Problem
You are given a binary string s.
You can perform the following operation on the string any number of times:
- Choose any index
ifrom the string 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 string or another'1'. For example, fors = "010010", if we choosei = 1, the resulting string will bes = "0** _001_** 10".
Return the maximum number of operations that you can perform.
Examples
Example 1
Input: s = "1001101"
Output: 4
Explanation:
We can perform the following operations:
* Choose index `i = 0`. The resulting string is `s = "_**001**_ 1101"`.
* Choose index `i = 4`. The resulting string is `s = "0011 _**01**_ 1"`.
* Choose index `i = 3`. The resulting string is `s = "001** _01_** 11"`.
* Choose index `i = 2`. The resulting string is `s = "00** _01_** 111"`.
Example 2
Input: s = "00111"
Output: 0
Constraints
1 <= s.length <= 10^5s[i]is either'0'or'1'.
Solution
Method 1 – Greedy Counting
Intuition
Each '1' can be moved to the end by swapping with '0's to its right. The number of operations is the sum of the number of '0's to the right of each '1'.
Approach
- Traverse the string from right to left, counting the number of '0's seen so far.
- For each '1', add the current count of '0's to the answer.
- Return the total sum as the answer.
Code
C++
class Solution {
public:
long long maximumOperations(string s) {
long long ans = 0, zeros = 0;
for (int i = s.size() - 1; i >= 0; --i) {
if (s[i] == '0') zeros++;
else ans += zeros;
}
return ans;
}
};
Go
func maximumOperations(s string) int64 {
ans, zeros := int64(0), int64(0)
for i := len(s) - 1; i >= 0; i-- {
if s[i] == '0' {
zeros++
} else {
ans += zeros
}
}
return ans
}
Java
class Solution {
public long maximumOperations(String s) {
long ans = 0, zeros = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == '0') zeros++;
else ans += zeros;
}
return ans;
}
}
Kotlin
class Solution {
fun maximumOperations(s: String): Long {
var ans = 0L
var zeros = 0L
for (i in s.length - 1 downTo 0) {
if (s[i] == '0') zeros++
else ans += zeros
}
return ans
}
}
Python
class Solution:
def maximumOperations(self, s: str) -> int:
ans = zeros = 0
for c in reversed(s):
if c == '0':
zeros += 1
else:
ans += zeros
return ans
Rust
impl Solution {
pub fn maximum_operations(s: String) -> i64 {
let mut ans = 0;
let mut zeros = 0;
for c in s.chars().rev() {
if c == '0' {
zeros += 1;
} else {
ans += zeros;
}
}
ans
}
}
TypeScript
class Solution {
maximumOperations(s: string): number {
let ans = 0, zeros = 0;
for (let i = s.length - 1; i >= 0; i--) {
if (s[i] === '0') zeros++;
else ans += zeros;
}
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(n), wherenis the length ofs, since we traverse the string once. - 🧺 Space complexity:
O(1), as we use only a few variables.