Maximum count of “010..” subsequences that can be removed from given Binary String
Last Updated :
15 Nov, 2021
Given a binary string S consisting of size N, the task is to find the maximum number of binary subsequences of the form "010.." of length at least 2 that can be removed from the given string S.
Examples:
Input: S = "110011010"
Output: 3
Explanation:
Following are the subsequence removed:
Operation 1: Choose the subsequence as "01" of indices {2, 4}, and deleting it modifies the string S = "1101010".
Operation 2: Choose the subsequence as "01" of indices {2, 3}, and deleting it modifies the string S = "11010".
Operation 3: Choose the subsequence as "01" of indices {2, 3}, and deleting it modifies the string S = "110".
From the above observations, the maximum number of times subsequence is removed is 3.
Input: S = "00111110011"
Output: 4
Approach: The given problem can be solved by removing the subsequence of type "01" every time to maximize the number of subsequences removed. Therefore, this can be maintained by keeping a variable that stores the count of the number of characters 0. Follow the steps below to solve the problem:
- Initialize a variable, say cnt as 0 to store the count of the number of 0s that have occurred in the string.
- Initialize variable, say ans as 0 to count the total number of removal operations performed.
- Traverse the string, S using the variable i and perform the following steps:
- If the value of S[i] is X then increment the value of cnt by 1.
- Otherwise, if the value of cnt is greater than 0, then decrement the value of cnt and increment the value of ans by 1.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <iostream>
using namespace std;
// Function to count the maximum number
// of operations performed on the string
void countOperations(string S)
{
// Size of the string
int n = S.length();
// Stores the maximum number of
// operations performed
int ans = 0;
// Stores the count of 'X' occurred
// so far
int cnt = 0;
// Traverse the string
for (int i = 0; i < n; i++) {
// Check if current char
// is 'X'
if (S[i] == '0') {
cnt++;
}
else {
// Check if the value of
// cnt is greater than 0
if (cnt > 0) {
// Decrement the value
// of cnt
cnt--;
// Increment the value
// of ans
ans++;
}
}
}
// Print the value of ans
cout << ans;
}
// Driver Code
int main()
{
string S = "110011010";
countOperations(S);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to count the maximum number
// of operations performed on the string
public static void countOperations(String S)
{
// Size of the string
int n = S.length();
// Stores the maximum number of
// operations performed
int ans = 0;
// Stores the count of 'X' occurred
// so far
int cnt = 0;
// Traverse the string
for (int i = 0; i < n; i++) {
// Check if current char
// is 'X'
if (S.charAt(i) == '0') {
cnt++;
}
else {
// Check if the value of
// cnt is greater than 0
if (cnt > 0) {
// Decrement the value
// of cnt
cnt--;
// Increment the value
// of ans
ans++;
}
}
}
// Print the value of ans
System.out.println(ans);
}
// Driver code
public static void main (String[] args)
{
String S = "110011010";
countOperations(S);
}
}
// This code is contributed by Manu Pathria
Python3
# Python program for the above approach
# Function to count the maximum number
# of operations performed on the string
def countOperations(S):
# Size of the string
n = len(S)
# Stores the maximum number of
# operations performed
ans = 0
# Stores the count of 'X' occurred
# so far
cnt = 0
# Traverse the string
for i in range(n):
# Check if current char
# is 'X'
if (S[i] == '0'):
cnt+=1
else:
# Check if the value of
# cnt is greater than 0
if (cnt > 0):
# Decrement the value
# of cnt
cnt-=1
# Increment the value
# of ans
ans+=1
# Print value of ans
print (ans)
# Driver Code
if __name__ == '__main__':
S = "110011010"
countOperations(S)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to count the maximum number
// of operations performed on the string
public static void countOperations(string S)
{
// Size of the string
int n = S.Length;
// Stores the maximum number of
// operations performed
int ans = 0;
// Stores the count of 'X' occurred
// so far
int cnt = 0;
// Traverse the string
for (int i = 0; i < n; i++) {
// Check if current char
// is 'X'
if (S[i] == '0') {
cnt++;
}
else {
// Check if the value of
// cnt is greater than 0
if (cnt > 0) {
// Decrement the value
// of cnt
cnt--;
// Increment the value
// of ans
ans++;
}
}
}
// Print the value of ans
Console.Write(ans);
}
// Driver code
public static void Main (string[] args)
{
string S = "110011010";
countOperations(S);
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// JavaScript Program for the above approach
// Function to count the maximum number
// of operations performed on the string
function countOperations(S) {
// Size of the string
let n = S.length;
// Stores the maximum number of
// operations performed
let ans = 0;
// Stores the count of 'X' occurred
// so far
let cnt = 0;
// Traverse the string
for (let i = 0; i < n; i++) {
// Check if current char
// is 'X'
if (S[i] == '0') {
cnt++;
}
else {
// Check if the value of
// cnt is greater than 0
if (cnt > 0) {
// Decrement the value
// of cnt
cnt--;
// Increment the value
// of ans
ans++;
}
}
}
// Print the value of ans
document.write(ans);
}
// Driver Code
let S = "110011010";
countOperations(S);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimize deletions in a Binary String to remove all subsequences of the form "0101" Given a binary string S of length N, the task is to find the minimum number of characters required to be deleted from the string such that no subsequence of the form â0101â exists in the string. Examples: Input: S = "0101101"Output: 2Explanation: Removing S[1] and S[5] modifies the string to 00111.
6 min read
Count binary Strings that does not contain given String as Subsequence Given a number N and string S, count the number of ways to create a binary string (containing only '0' and '1') of size N such that it does not contain S as a subsequence. Examples: Input: N = 3, S = "10".Output: 4Explanation: There are 8 strings possible to fill 3 positions with 0's or 1's. {"000",
15+ min read
Minimum count of 0s to be removed from given Binary string to make all 1s occurs consecutively Given a binary string S of size N, the task is to find the minimum numbers of 0s that must be removed from the string S such that all the 1s occurs consecutively. Examples: Input: S = "010001011" Output: 4 Explanation: Removing the characters { S[2], S[3], S[4], S[6] } from the string S modifies the
7 min read
Replace '?' to convert given string to a binary string with maximum count of '0' and "10" Given string str, consisting of three different types of characters '0', '1' and '?', the task is to convert the given string to a binary string by replacing the '?' characters with either '0' or '1' such that the count of 0s and 10 in the binary string is maximum. Examples: Input: str = 10?0?11Outp
4 min read
Minimum number of palindromic subsequences to be removed to empty a binary string Given a binary string, count minimum number of subsequences to be removed to make it an empty string. Examples : Input: str[] = "10001" Output: 1 Since the whole string is palindrome, we need only one removal. Input: str[] = "10001001" Output: 2 We can remove the middle 1 as first removal, after fir
6 min read