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. 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
**0**s and**1**s of a block using partial sum. - Fix the starting and ending indices of blocks of
**0**s and**1**s 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++

`// 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'` `;` `}` |

*chevron_right*

*filter_none*

## Java

`// 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` |

*chevron_right*

*filter_none*

## Python3

`# 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` |

*chevron_right*

*filter_none*

## C#

`// 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` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(N^{2})**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Deletions of "01" or "10" in binary string to make it free from "01" or "10"
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Maximize cost of deletions to obtain string having no pair of similar adjacent characters
- Minimum number of deletions to make a string palindrome
- Minimum number of deletions so that no two consecutive are same
- Minimum deletions required such that any number X will occur exactly X times
- Minimize sum of smallest elements from K subsequences of length L
- Minimize cost to convert all characters of a binary string to 0s
- Number of subsequences of the form a^i b^j c^k
- Count of subsequences of length 4 in form (x, x, x+1, x+1) | Set 2
- Minimize length of prefix of string S containing all characters of another string T
- Number of subsequences in a given binary string divisible by 2
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Form N-copy string with add, remove and append operations
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Print all subsequences of a string | Iterative Method
- Print all subsequences of a string using ArrayList

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.