Minimum number of alternate subsequences required to be removed to empty a Binary String
Last Updated :
22 Sep, 2021
Given a binary string S consisting of N characters, the task is to print the minimum number of operations required to remove all the characters from the given string S by removing a single character or removing any subsequence of alternate characters in each operation.
Examples:
Input: S = “010101”
Output: 1
Explanation:
Below are the operations performed:
Operation 1: Consider the subsequence S[0, 5] i.e., “010101” as it contains alternating characters. Therefore, removing this modifies the string to “”.
Hence, the total number of operation required is 1.
Input: S = “00011”
Output: 3
Approach: The given problem can be solved by iterating over the string once and keep track of the maximum number of remaining 0s and 1s. Follow the steps below to solve the problem:
- Initialize a variable, say ans to store the maximum number of 0s and 1s which are still left to be removed, a variable cnt0 to count the number of 0s, and a variable cnt1 to count the number of 1s.
- Traverse the given string S from the beginning and perform the following steps:
- If the current character is 0, then increment the value of cnt0 by 1 and decrement the value of cnt1 by 1 if it is greater than 0.
- If the current character is 1, increment the value of cnt1 by 1 and decrement the value of cnt0 by 1 if it is greater than 0.
- Update the value of ans to the maximum of ans, cnt1, and cnt0.
- After completing the above steps, print the value of ans as the minimum number of operations required to remove all the characters.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOpsToEmptyString(string s)
{
int ans = INT_MIN;
int cn0 = 0;
int cn1 = 0;
for ( int i = 0;
i < s.length(); i++) {
if (s[i] == '0' ) {
if (cn1 > 0)
cn1--;
cn0++;
}
else {
if (cn0 > 0)
cn0--;
cn1++;
}
ans = max({ ans, cn0, cn1 });
}
cout << ans;
}
int main()
{
string S = "010101" ;
minOpsToEmptyString(S);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void minOpsToEmptyString(String s)
{
int ans = Integer.MIN_VALUE;
int cn0 = 0 ;
int cn1 = 0 ;
for ( int i = 0 ; i < s.length(); i++)
{
if (s.charAt(i) == '0' )
{
if (cn1 > 0 )
cn1--;
cn0++;
}
else
{
if (cn0 > 0 )
cn0--;
cn1++;
}
ans = Math.max(ans, Math.max(cn0, cn1));
}
System.out.print(ans);
}
public static void main(String[] args)
{
String S = "010101" ;
minOpsToEmptyString(S);
}
}
|
Python3
def minOpsToEmptyString(s):
ans = - 10 * * 9
cn0 = 0
cn1 = 0
for i in range ( len (s)):
if (s[i] = = '0' ):
if (cn1 > 0 ):
cn1 - = 1
cn0 + = 1
else :
if (cn0 > 0 ):
cn0 - = 1
cn1 + = 1
ans = max ([ans, cn0, cn1])
print (ans)
if __name__ = = '__main__' :
S = "010101"
minOpsToEmptyString(S)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void minOpsToEmptyString( string s)
{
int ans = 0;
int cn0 = 0;
int cn1 = 0;
for ( int i = 0; i < s.Length; i++)
{
if (s[i] == '0' )
{
if (cn1 > 0)
cn1--;
cn0++;
}
else
{
if (cn0 > 0)
cn0--;
cn1++;
}
ans = Math.Max(ans, Math.Max(cn0, cn1));
}
Console.Write(ans);
}
public static void Main()
{
string S = "010101" ;
minOpsToEmptyString(S);
}
}
|
Javascript
<script>
function minOpsToEmptyString(s)
{
var ans = Number.MIN_VALUE;
var cn0 = 0;
var cn1 = 0;
for ( var i = 0; i < s.length; i++)
{
if (s.charAt(i) == '0' )
{
if (cn1 > 0)
cn1--;
cn0++;
}
else
{
if (cn0 > 0)
cn0--;
cn1++;
}
ans = Math.max(ans, Math.max(cn0, cn1));
}
document.write(ans);
}
var S = "010101" ;
minOpsToEmptyString(S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...