Open In App

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: 2
Explanation: Removing S[1] and S[5] modifies the string to 00111. Therefore, no subsequence of the type 0101 can be obtained from the given string.

Input: S = “0110100110”
Output: 2



 

Approach: Follow the steps below to solve the problem:

Below is the implementation of the above approach: 
 




// C++ Program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum characters
// to be removed such that no subsequence
// of the form "0101" exists in the string
int findmin(string s)
{
    int n = s.length();
    int i, j, maximum = 0;
 
    // Stores the partial sums
    int incr[n + 1] = { 0 };
 
    for (i = 0; i < n; i++) {
 
        // Calculate partial sums
        incr[i + 1] = incr[i];
 
        if (s[i] == '0') {
            incr[i + 1]++;
        }
    }
 
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
 
            // Setting endpoints and
            // deleting characters indices.
            maximum
                = max(maximum, incr[i] + j - i + 1
                                   - (incr[j + 1] - incr[i])
                                   + incr[n] - incr[j + 1]);
        }
    }
 
    // Return count of deleted characters
    return n - maximum;
}
// Driver Code
int main()
{
    string S = "0110100110";
    int minimum = findmin(S);
    cout << minimum << '\n';
}




// Java Program to implement
// the above approach
import java.io.*;
class GFG{
 
// Function to find minimum
// characters to be removed
// such that no subsequence
// of the form "0101" exists
// in the string
static int findmin(String s)
{
  int n = s.length();
  int i, j, maximum = 0;
 
  // Stores the partial sums
  int[] incr = new int[n + 1];
 
  for (i = 0; i < n; i++)
  {
    // Calculate partial sums
    incr[i + 1] = incr[i];
 
    if (s.charAt(i) == '0')
    {
      incr[i + 1]++;
    }
  }
 
  for (i = 0; i < n; i++)
  {
    for (j = i + 1; j < n; j++)
    {
      // Setting endpoints and
      // deleting characters indices.
      maximum = Math.max(maximum, incr[i] +
                         j - i + 1 -
                        (incr[j + 1] - incr[i]) +
                         incr[n] - incr[j + 1]);
    }
  }
 
  // Return count of
  // deleted characters
  return n - maximum;
}
   
// Driver Code
public static void main(String[] args)
{
  String S = "0110100110";
  int minimum = findmin(S);
  System.out.println(minimum);
}
}
 
// This code is contributed by akhilsaini




# Python3 Program to implement
# the above approach
 
# Function to find minimum
# characters to be removed
# such that no subsequence
# of the form "0101" exists
# in the string
def findmin(s):
 
    n = len(s)
    maximum = 0
 
    # Stores the partial sums
    incr = [0] * (n + 1)
     
    for i in range(0, n):
       
        # Calculate partial sums
        incr[i + 1] = incr[i]
 
        if (s[i] == '0'):
            incr[i + 1] = incr[i + 1] + 1
 
    for i in range(0, n + 1):
        for j in range(i + 1, n):
 
            # Setting endpoints and
            # deleting characters indices.
            maximum = max(maximum, incr[i] +
                          j - i + 1 -
                         (incr[j + 1] - incr[i]) +
                          incr[n] - incr[j + 1])
 
    # Return count of
    # deleted characters
    return n - maximum
 
# Driver Code
if __name__ == "__main__":
 
    S = "0110100110"
    minimum = findmin(S)
    print(minimum)
 
# This code is contributed by akhilsaini




// C# Program to implement
// the above approach
using System;
class GFG{
 
// Function to find minimum
// characters to be removed
// such that no subsequence
// of the form "0101" exists
// in the string
static int findmin(string s)
{
  int n = s.Length;
  int i, j, maximum = 0;
 
  // Stores the partial sums
  int[] incr = new int[n + 1];
 
  for (i = 0; i < n; i++)
  {
    // Calculate partial sums
    incr[i + 1] = incr[i];
 
    if (s[i] == '0')
    {
      incr[i + 1]++;
    }
  }
 
  for (i = 0; i < n; i++)
  {
    for (j = i + 1; j < n; j++)
    {
      // Setting endpoints and
      // deleting characters indices.
      maximum = Math.Max(maximum, incr[i] +
                         j - i + 1 -
                        (incr[j + 1] - incr[i]) +
                         incr[n] - incr[j + 1]);
    }
  }
 
  // Return count of
  // deleted characters
  return n - maximum;
}
   
// Driver Code
public static void Main()
{
  string S = "0110100110";
  int minimum = findmin(S);
  Console.WriteLine(minimum);
}
}
 
// This code is contributed by akhilsaini




<script>
 
// JavaScript Program to implement
// the above approach
 
 
// Function to find minimum characters
// to be removed such that no subsequence
// of the form "0101" exists in the string
function findmin(s)
{
    let n = s.length;
    let i, j, maximum = 0;
 
    // Stores the partial sums
  
     var incr = new Array(n+1);
      incr.fill(0);
    for (i = 0; i < n; i++) {
 
        // Calculate partial sums
        incr[i + 1] = incr[i];
 
        if (s[i] == '0') {
            incr[i + 1]++;
        }
    }
 
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
 
            // Setting endpoints and
            // deleting characters indices.
            maximum
                = Math.max(maximum, incr[i] + j - i + 1
                                - (incr[j + 1] - incr[i])
                                + incr[n] - incr[j + 1]);
        }
    }
 
    // Return count of deleted characters
    return n - maximum;
}
// Driver Code
 
    let S = "0110100110";
    let minimum = findmin(S);
    document.write(minimum + '\n');
     
</script>

Output: 
3

 

Time Complexity: O(N2)
Auxiliary Space: O(N)


Article Tags :