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); } |
3
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
Recommended Posts:
- Minimum removal of consecutive similar characters required to empty a Binary String
- Longest subarray of non-empty cells after removal of at most a single empty cell
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Minimum removal of characters required such that permutation of given string is a palindrome
- Maximum removal from array when removal time >= waiting time
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Sum of all possible strings obtained by removal of non-empty substrings
- Minimum number of subsequences required to convert one string to another
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Minimum addition/removal of characters to be done to make frequency of each character prime
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Find distinct characters in distinct substrings of a string
- Maximum length of balanced string after swapping and removal of characters
- Lexicographically largest possible String after removal of K characters
- Generating distinct subsequences of a given string in lexicographic order
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Count minimum number of subsets (or subsequences) with consecutive numbers
- String with k distinct characters and no same characters adjacent
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

