Given a binary string, str, the task is to empty the given string by minimum number of removals of a single character or a subsequence containing distinct consecutive characters from str.
Examples:
Input: str = “0100100111”
Output: 3
Explanation:
Removing the subsequence “010101” from the string modifies str to “0011”.
Removing the subsequence “01” from the string modifies str to “01”.
Removing the subsequence “01” from the string modifies str to “” which is an empty string.
Therefore, the required output is 3.Input: str = “010110”
Output: 2
Naive Approach: The simplest approach to solve this problem is to traverse the string repetitively and remove the longest subsequence of distinct consecutive characters from the string and increment the count after every removal. Finally, print the count.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:
- Initialize two variables, say cntOne and cntZero, to store the count of 1s and 0s.
- Traverse the string using variable i and check the following conditions:
- If str[i] == ‘0’, then increment the value of cntZero and check if the value of cntOne is greater than 0 or not. If found to be true, then decrement the value of cntOne.
- If str[i] == ‘1’, then increment the value of cntOne and check if the value of cntZero is greater than 0 or not. If found to be true, then decrement the value of cntZero.
- Finally, print the value of (cntZero + cntOne).
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; //Function to count minimum operations required // to make the string an empty string int findMinOperationsReqEmpStr(string str) { // Stores count of 1s by removing // consecutive distinct subsequence int cntOne = 0; // Stores count of 0s by removing // consecutive distinct subsequence int cntZero = 0 ; // Stores length of str int N = str.length(); // Traverse the string for ( int i = 0; i < N; i++) { // If current character // is 0 if (str[i] == '0' ){ if (cntOne) { // Update cntOne cntOne--; } // Update cntZero cntZero++; } // If current character // is 1 else { //Update cntZero if (cntZero) { cntZero--; } // Update cntOne cntOne++; } } return (cntOne + cntZero); } // Driver Code int main() { string str = "0100100111" ; cout<< findMinOperationsReqEmpStr(str); } |
Java
// Java program to implement // the above approach import java.util.*; import java.lang.*; class GFG{ //Function to count minimum operations required // to make the string an empty string static int findMinOperationsReqEmpStr(String str) { // Stores count of 1s by removing // consecutive distinct subsequence int cntOne = 0 ; // Stores count of 0s by removing // consecutive distinct subsequence int cntZero = 0 ; // Stores length of str int N = str.length(); // Traverse the string for ( int i = 0 ; i < N; i++) { // If current character // is 0 if (str.charAt(i) == '0' ) { if (cntOne != 0 ) { // Update cntOne cntOne--; } // Update cntZero cntZero++; } // If current character // is 1 else { // Update cntZero if (cntZero != 0 ) { cntZero--; } // Update cntOne cntOne++; } } return (cntOne + cntZero); } // Driver code public static void main(String[] args) { String str = "0100100111" ; System.out.print(findMinOperationsReqEmpStr(str)); } } // This code is contributed by ajaykr00kj |
Python3
# Python3 program to implement # the above approach # Function to count minimum operations # required to make the string an empty # string def findMinOperationsReqEmpStr( str ): # Stores count of 1s by removing # consecutive distinct subsequence cntOne = 0 # Stores count of 0s by removing # consecutive distinct subsequence cntZero = 0 # Traverse the string for element in str : # If current character # is 0 if element = = '0' : if cntOne > 0 : # Update cntOne cntOne = cntOne - 1 # Update cntZero cntZero = cntZero + 1 # If current character # is 1 else : # Update cntZero if cntZero > 0 : cntZero = cntZero - 1 # Update cntOne cntOne = cntOne + 1 return cntOne + cntZero # Driver code if __name__ = = "__main__" : str = "0100100111" print (findMinOperationsReqEmpStr( str )) # This code is contributed by ajaykr00kj |
C#
// C# program to implement // the above approach using System; class GFG { // Function to count minimum operations required // to make the string an empty string static int findMinOperationsReqEmpStr(String str) { // Stores count of 1s by removing // consecutive distinct subsequence int cntOne = 0; // Stores count of 0s by removing // consecutive distinct subsequence int cntZero = 0; // Stores length of str int N = str.Length; // Traverse the string for ( int i = 0; i < N; i++) { // If current character // is 0 if (str[i] == '0' ) { if (cntOne != 0) { // Update cntOne cntOne--; } // Update cntZero cntZero++; } // If current character // is 1 else { // Update cntZero if (cntZero != 0) { cntZero--; } // Update cntOne cntOne++; } } return (cntOne + cntZero); } // Driver code public static void Main(String[] args) { String str = "0100100111" ; Console.Write(findMinOperationsReqEmpStr(str)); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // Javascript program to implement // the above approach // Function to count minimum operations required // to make the string an empty string function findMinOperationsReqEmpStr(str) { // Stores count of 1s by removing // consecutive distinct subsequence let cntOne = 0; // Stores count of 0s by removing // consecutive distinct subsequence let cntZero = 0; // Stores length of str let N = str.length; // Traverse the string for (let i = 0; i < N; i++) { // If current character // is 0 if (str[i] == '0' ) { if (cntOne != 0) { // Update cntOne cntOne--; } // Update cntZero cntZero++; } // If current character // is 1 else { // Update cntZero if (cntZero != 0) { cntZero--; } // Update cntOne cntOne++; } } return (cntOne + cntZero); } // Driver code let str = "0100100111" ; document.write(findMinOperationsReqEmpStr(str)); // This code is contributed by code_hunt </script> |
3
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!