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:
- The required valid string can consist of at most three blocks of the same elements i.e. the strings can be one of the following patterns “00…0”, “11…1”, “00…01…1”, “1…10..0”, “00..01…10..0”, “1…10…01…1”.
- Count frequencies of 0s and 1s of a block using partial sum.
- Fix the starting and ending indices of blocks of 0s and 1s and determine the minimum number of characters required to be deleted by the calculated partial sums.
- Therefore, check the length of the longest string that can be obtained by removing the subsequences of the given type.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findmin(string s)
{
int n = s.length();
int i, j, maximum = 0;
int incr[n + 1] = { 0 };
for (i = 0; i < n; i++) {
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++) {
maximum
= max(maximum, incr[i] + j - i + 1
- (incr[j + 1] - incr[i])
+ incr[n] - incr[j + 1]);
}
}
return n - maximum;
}
int main()
{
string S = "0110100110" ;
int minimum = findmin(S);
cout << minimum << '\n' ;
}
|
Java
import java.io.*;
class GFG{
static int findmin(String s)
{
int n = s.length();
int i, j, maximum = 0 ;
int [] incr = new int [n + 1 ];
for (i = 0 ; i < n; i++)
{
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++)
{
maximum = Math.max(maximum, incr[i] +
j - i + 1 -
(incr[j + 1 ] - incr[i]) +
incr[n] - incr[j + 1 ]);
}
}
return n - maximum;
}
public static void main(String[] args)
{
String S = "0110100110" ;
int minimum = findmin(S);
System.out.println(minimum);
}
}
|
Python3
def findmin(s):
n = len (s)
maximum = 0
incr = [ 0 ] * (n + 1 )
for i in range ( 0 , n):
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):
maximum = max (maximum, incr[i] +
j - i + 1 -
(incr[j + 1 ] - incr[i]) +
incr[n] - incr[j + 1 ])
return n - maximum
if __name__ = = "__main__" :
S = "0110100110"
minimum = findmin(S)
print (minimum)
|
C#
using System;
class GFG{
static int findmin( string s)
{
int n = s.Length;
int i, j, maximum = 0;
int [] incr = new int [n + 1];
for (i = 0; i < n; i++)
{
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++)
{
maximum = Math.Max(maximum, incr[i] +
j - i + 1 -
(incr[j + 1] - incr[i]) +
incr[n] - incr[j + 1]);
}
}
return n - maximum;
}
public static void Main()
{
string S = "0110100110" ;
int minimum = findmin(S);
Console.WriteLine(minimum);
}
}
|
Javascript
<script>
function findmin(s)
{
let n = s.length;
let i, j, maximum = 0;
var incr = new Array(n+1);
incr.fill(0);
for (i = 0; i < n; i++) {
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++) {
maximum
= Math.max(maximum, incr[i] + j - i + 1
- (incr[j + 1] - incr[i])
+ incr[n] - incr[j + 1]);
}
}
return n - maximum;
}
let S = "0110100110" ;
let minimum = findmin(S);
document.write(minimum + '\n' );
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)