3228. Maximum Number of Operations to Move Ones to the End LeetCode Solution

In this guide, you will get 3228. Maximum Number of Operations to Move Ones to the End LeetCode Solution with the best time and space complexity. The solution to Maximum Number of Operations to Move Ones to the End problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Number of Operations to Move Ones to the End solution in C++
  4. Maximum Number of Operations to Move Ones to the End solution in Java
  5. Maximum Number of Operations to Move Ones to the End solution in Python
  6. Additional Resources
3228. Maximum Number of Operations to Move Ones to the End LeetCode Solution image

Problem Statement of 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'. For example, for s = "010010", if we choose i = 1, the resulting string will be s = "000110".

Return the maximum number of operations that you can perform.

Example 1:

Input: s = “1001101”
Output: 4
Explanation:
We can perform the following operations:

Choose index i = 0. The resulting string is s = “0011101”.
Choose index i = 4. The resulting string is s = “0011011”.
Choose index i = 3. The resulting string is s = “0010111”.
Choose index i = 2. The resulting string is s = “0001111”.

See also  2207. Maximize Number of Subsequences in a String LeetCode Solution

Example 2:

Input: s = “00111”
Output: 0

Constraints:

1 <= s.length <= 105
s[i] is either '0' or '1'.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

3228. Maximum Number of Operations to Move Ones to the End LeetCode Solution in C++

class Solution {
 public:
  int maxOperations(string s) {
    int ans = 0;
    int ones = 0;

    for (int i = 0; i < s.length(); ++i)
      if (s[i] == '1')
        ++ones;
      else if (i + 1 == s.length() || s[i + 1] == '1')
        ans += ones;

    return ans;
  }
};
/* code provided by PROGIEZ */

3228. Maximum Number of Operations to Move Ones to the End LeetCode Solution in Java

class Solution {
  public int maxOperations(String s) {
    int ans = 0;
    int ones = 0;

    for (int i = 0; i < s.length(); ++i)
      if (s.charAt(i) == '1')
        ++ones;
      else if (i == s.length() - 1 || s.charAt(i + 1) == '1')
        ans += ones;

    return ans;
  }
}
// code provided by PROGIEZ

3228. Maximum Number of Operations to Move Ones to the End LeetCode Solution in Python

class Solution:
  def maxOperations(self, s: str) -> int:
    ans = 0
    ones = 0

    for i, c in enumerate(s):
      if c == '1':
        ones += 1
      elif i + 1 == len(s) or s[i + 1] == '1':
        ans += ones

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.