Given a binary string S of length N, the task is to minimize the count of repetitive removal of alternating subsequence of the 0 and 1 from the given binary string S to make the string empty.
Examples:
Input: S = “0100100111”
Output: 3
Explanation:
Remove subsequence “010101” from S to modify it to “0011”.
Remove “01” from “0011” to make it “01”.
Finally, remove “01” to make it an empty string.Input: S = “1111”
Output: 4
Approach: The given problem can be solved by observing that an alternating subsequence of 0 and 1 is to be removed and to remove all the consecutive characters 1s or 0s can only be removed at each separate operation, not in a single operation.
Therefore, the minimum number of operations required is the maximum count of consecutive 0s and 1s.
Below is the implementation of the above approach:
C++
#include <iostream>using namespace std;void minOpsToEmptyString(string S, int N){ // Initialize variables int one = 0, zero = 0; int x0 = 0, x1 = 0; // Traverse the string for (int i = 0; i < N; i++) { // If current character is 0 if (S[i] == '0') { x0++; x1 = 0; } else { x1++; x0 = 0; } // Update maximum consecutive // 0s and 1s zero = max(x0, zero); one = max(x1, one); } // Print the minimum operation cout << max(one, zero) << endl;}// Driver code+int main(){ // input string string S = "0100100111"; // length of string int N = S.length(); // Function Call minOpsToEmptyString(S, N);}// This code is contributed by aditya7409 |
Java
// Java program for the above approachimport java.io.*;import java.util.*;class GFG { // Function to find the minimum // number of operations required // to empty the string public static void minOpsToEmptyString(String S, int N) { // Initialize variables int one = 0, zero = 0; int x0 = 0, x1 = 0; // Traverse the string for (int i = 0; i < N; i++) { // If current character is 0 if (S.charAt(i) == '0') { x0++; x1 = 0; } else { x1++; x0 = 0; } // Update maximum consecutive // 0s and 1s zero = Math.max(x0, zero); one = Math.max(x1, one); } // Print the minimum operation System.out.println( Math.max(one, zero)); } // Driver Code public static void main(String[] args) { String S = "0100100111"; int N = S.length(); // Function Call minOpsToEmptyString(S, N); }} |
Python3
# Python3 program for the above approachdef minOpsToEmptyString(S, N): # Initialize variables one = 0 zero = 0 x0 = 0 x1 = 0 # Traverse the string for i in range(N): # If current character is 0 if (S[i] == '0'): x0 += 1 x1 = 0 else: x1 += 1 x0 = 0 # Update maximum consecutive # 0s and 1s zero = max(x0, zero) one = max(x1, one) # Print the minimum operation print(max(one, zero))# Driver code+if __name__ == "__main__": # input string S = "0100100111" # length of string N = len(S) # Function Call minOpsToEmptyString(S, N) # This code is contributed by chitranayal |
C#
// C# program for the above approachusing System;class GFG{ // Function to find the minimum // number of operations required // to empty the string public static void minOpsToEmptyString(string S, int N) { // Initialize variables int one = 0, zero = 0; int x0 = 0, x1 = 0; // Traverse the string for (int i = 0; i < N; i++) { // If current character is 0 if (S[i] == '0') { x0++; x1 = 0; } else { x1++; x0 = 0; } // Update maximum consecutive // 0s and 1s zero = Math.Max(x0, zero); one = Math.Max(x1, one); } // Print the minimum operation Console.WriteLine(Math.Max(one, zero)); } // Driver Code static public void Main() { string S = "0100100111"; int N = S.Length; // Function Call minOpsToEmptyString(S, N); }}// This code is contributed by Dharanendra L V |
Javascript
<script>// Javascript program of the above approach// Function to find the minimum// number of operations required// to empty the stringfunction minOpsToEmptyString(S, N){ // Initialize variables let one = 0, zero = 0; let x0 = 0, x1 = 0; // Traverse the string for(let i = 0; i < N; i++) { // If current character is 0 if (S[i] == '0') { x0++; x1 = 0; } else { x1++; x0 = 0; } // Update maximum consecutive // 0s and 1s zero = Math.max(x0, zero); one = Math.max(x1, one); } // Print the minimum operation document.write(Math.max(one, zero));}// Driver Codelet S = "0100100111";let N = S.length;// Function CallminOpsToEmptyString(S, N);// This code is contributed by chinmoy1997pal</script> |
3
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live


