Minimum removal of subsequences of distinct consecutive characters required to empty a given string

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


Output:

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Still Learner

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.