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:
- 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++
// 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
// 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
# 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#
// 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 |
Javascript
<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)