Minimum removal of subsequences of distinct consecutive characters required to empty a given string
Last Updated :
21 Apr, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int findMinOperationsReqEmpStr(string str)
{
int cntOne = 0;
int cntZero = 0 ;
int N = str.length();
for ( int i = 0; i < N; i++) {
if (str[i] == '0' ){
if (cntOne) {
cntOne--;
}
cntZero++;
}
else {
if (cntZero) {
cntZero--;
}
cntOne++;
}
}
return (cntOne + cntZero);
}
int main()
{
string str = "0100100111" ;
cout<< findMinOperationsReqEmpStr(str);
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static int findMinOperationsReqEmpStr(String str)
{
int cntOne = 0 ;
int cntZero = 0 ;
int N = str.length();
for ( int i = 0 ; i < N; i++)
{
if (str.charAt(i) == '0' )
{
if (cntOne != 0 )
{
cntOne--;
}
cntZero++;
}
else
{
if (cntZero != 0 )
{
cntZero--;
}
cntOne++;
}
}
return (cntOne + cntZero);
}
public static void main(String[] args)
{
String str = "0100100111" ;
System.out.print(findMinOperationsReqEmpStr(str));
}
}
|
Python3
def findMinOperationsReqEmpStr( str ):
cntOne = 0
cntZero = 0
for element in str :
if element = = '0' :
if cntOne > 0 :
cntOne = cntOne - 1
cntZero = cntZero + 1
else :
if cntZero > 0 :
cntZero = cntZero - 1
cntOne = cntOne + 1
return cntOne + cntZero
if __name__ = = "__main__" :
str = "0100100111"
print (findMinOperationsReqEmpStr( str ))
|
C#
using System;
class GFG
{
static int findMinOperationsReqEmpStr(String str)
{
int cntOne = 0;
int cntZero = 0;
int N = str.Length;
for ( int i = 0; i < N; i++)
{
if (str[i] == '0' )
{
if (cntOne != 0)
{
cntOne--;
}
cntZero++;
}
else
{
if (cntZero != 0)
{
cntZero--;
}
cntOne++;
}
}
return (cntOne + cntZero);
}
public static void Main(String[] args)
{
String str = "0100100111" ;
Console.Write(findMinOperationsReqEmpStr(str));
}
}
|
Javascript
<script>
function findMinOperationsReqEmpStr(str)
{
let cntOne = 0;
let cntZero = 0;
let N = str.length;
for (let i = 0; i < N; i++)
{
if (str[i] == '0' )
{
if (cntOne != 0)
{
cntOne--;
}
cntZero++;
}
else
{
if (cntZero != 0)
{
cntZero--;
}
cntOne++;
}
}
return (cntOne + cntZero);
}
let str = "0100100111" ;
document.write(findMinOperationsReqEmpStr(str));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...