Given binary string str of length N, the task is to find the minimum number of characters required to be deleted from the given binary string to make a substring of 0s followed by a substring of 1s.
Examples:
Input: str = “00101101”
Output: 2
Explanation: Removing str[2] and str[6] or removing str[3] and str[6] modifies the given binary string to “000111” or “001111” respectively. The number of removals required in both the cases is 2, which is the minimum possible.Input: str = “111000001111”
Output: 3
Naive Approach: The simplest approach is to solve this problem is to traverse the string and for every ‘1’ encountered, calculate the minimum number of deletions required by deleting 0s or deleting 1s. Finally, print the minimum deletions required.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by having an auxiliary space that keeps the count of the number of 0s after 1s. Using this pre-computation, the time complexity can be improved by a factor of N. Below are the steps:
- Initialize a variable, say ans, to store the minimum number of characters required to be deleted.
- Initialize an array, say zeroCount[], to count the number of 0s present after a given index.
- Traverse the string str from the end over the range [N – 2, 0] and if the current character is 0, then update zeroCount[i] as (zeroCount[i + 1] + 1). Otherwise, update zeroCount[i] as zeroCount[i + 1].
- Initialize a variable, say oneCount, to count the number of 1s.
- Traverse the given string again. For every character found to be ‘1’, update ans as the minimum of ans and (oneCount + zeroCount[i]).
- After the above steps, if the value of ans remains same as its initialized value, then 0 characters are required to be deleted. Otherwise, ans is the required number of characters to be deleted.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> #include <iostream> using namespace std; // Function to count minimum removals // required to make a given string // concatenation of substring of 0s // followed by substring of 1s int minimumDeletions(string s) { // Stores the length of the string int n = s.size(); // Precompute the count of 0s int zeroCount[n]; // Check for the last character zeroCount[n - 1] = (s[n - 1] == '0' ) ? 1 : 0; // Traverse and update zeroCount array for ( int i = n - 2; i >= 0; i--) // If current character is 0, zeroCount[i] = (s[i] == '0' ) ? // Update aCount[i] as // aCount[i + 1] + 1 zeroCount[i + 1] + 1 : // Update as aCount[i + 1] zeroCount[i + 1]; // Keeps track of deleted 1s int oneCount = 0; // Stores the count of removals int ans = INT_MAX; // Traverse the array for ( int i = 0; i < n; i++) { // If current character is 1 if (s[i] == '1' ) { // Update ans ans = min(ans, oneCount + zeroCount[i]); oneCount++; } } // If all 1s are deleted ans = min(ans, oneCount); // Return the minimum // number of deletions return (ans == INT_MAX) ? 0 : ans; } // Driver Code int main() { string stri = "00101101" ; cout << minimumDeletions(stri) << endl; return 0; } // This code is contributed by AnkThon |
Java
// Java program for the above approach import java.io.*; class GFG { // Function to count minimum removals // required to make a given string // concatenation of substring of 0s // followed by substring of 1s public static int minimumDeletions(String s) { // Stores the length of the string int n = s.length(); // Precompute the count of 0s int zeroCount[] = new int [n]; // Check for the last character zeroCount[n - 1 ] = (s.charAt(n - 1 ) == '0' ) ? 1 : 0 ; // Traverse and update zeroCount array for ( int i = n - 2 ; i >= 0 ; i--) // If current character is 0, zeroCount[i] = (s.charAt(i) == '0' ) // Update aCount[i] as // aCount[i + 1] + 1 ? zeroCount[i + 1 ] + 1 // Update as aCount[i + 1] : zeroCount[i + 1 ]; // Keeps track of deleted 1s int oneCount = 0 ; // Stores the count of removals int ans = Integer.MAX_VALUE; // Traverse the array for ( int i = 0 ; i < n; i++) { // If current character is 1 if (s.charAt(i) == '1' ) { // Update ans ans = Math.min(ans, oneCount + zeroCount[i]); oneCount++; } } // If all 1s are deleted ans = Math.min(ans, oneCount); // Return the minimum // number of deletions return (ans == Integer.MAX_VALUE) ? 0 : ans; } // Driver Code public static void main(String[] args) { String str = "00101101" ; System.out.println( minimumDeletions(str)); } } |
Python3
# Python3 program for the above approach # Function to count minimum removals # required to make a given string # concatenation of substring of 0s # followed by substring of 1s def minimumDeletions(s): # Stores the length of the string n = len (s) # Precompute the count of 0s zeroCount = [ 0 for i in range (n)] # Check for the last character zeroCount[n - 1 ] = 1 if s[n - 1 ] = = '0' else 0 # Traverse and update zeroCount array for i in range (n - 2 , - 1 , - 1 ): # If current character is 0, zeroCount[i] = zeroCount[i + 1 ] + 1 if (s[i] = = '0' ) else zeroCount[i + 1 ] # Keeps track of deleted 1s oneCount = 0 # Stores the count of removals ans = 10 * * 9 # Traverse the array for i in range (n): # If current character is 1 if (s[i] = = '1' ): # Update ans ans = min (ans,oneCount + zeroCount[i]) oneCount + = 1 # If all 1s are deleted ans = min (ans, oneCount) # Return the minimum # number of deletions return 0 if ans = = 10 * * 18 else ans # Driver Code if __name__ = = '__main__' : str = "00101101" print (minimumDeletions( str )) # This code is contributed by mohit kumar 29 |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG { // Function to count minimum removals // required to make a given string // concatenation of substring of 0s // followed by substring of 1s public static int minimumDeletions(String s) { // Stores the length of the string int n = s.Length; // Precompute the count of 0s int []zeroCount = new int [n]; // Check for the last character zeroCount[n - 1] = (s[n - 1] == '0' ) ? 1 : 0; // Traverse and update zeroCount array for ( int i = n - 2; i >= 0; i--) // If current character is 0, zeroCount[i] = (s[i] == '0' ) // Update aCount[i] as // aCount[i + 1] + 1 ? zeroCount[i + 1] + 1 // Update as aCount[i + 1] : zeroCount[i + 1]; // Keeps track of deleted 1s int oneCount = 0; // Stores the count of removals int ans = int .MaxValue; // Traverse the array for ( int i = 0; i < n; i++) { // If current character is 1 if (s[i] == '1' ) { // Update ans ans = Math.Min(ans, oneCount + zeroCount[i]); oneCount++; } } // If all 1s are deleted ans = Math.Min(ans, oneCount); // Return the minimum // number of deletions return (ans == int .MaxValue) ? 0 : ans; } // Driver Code public static void Main(String[] args) { String str = "00101101" ; Console.WriteLine( minimumDeletions(str)); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // JavaScript program for above approach // Function to count minimum removals // required to make a given string // concatenation of substring of 0s // followed by substring of 1s function minimumDeletions(s) { // Stores the length of the string let n = s.length; // Precompute the count of 0s let zeroCount = []; // Check for the last character zeroCount[n - 1] = (s[n - 1] == '0' ) ? 1 : 0; // Traverse and update zeroCount array for (let i = n - 2; i >= 0; i--) // If current character is 0, zeroCount[i] = (s[i] == '0' ) // Update aCount[i] as // aCount[i + 1] + 1 ? zeroCount[i + 1] + 1 // Update as aCount[i + 1] : zeroCount[i + 1]; // Keeps track of deleted 1s let oneCount = 0; // Stores the count of removals let ans = Number.MAX_VALUE; // Traverse the array for (let i = 0; i < n; i++) { // If current character is 1 if (s[i] == '1' ) { // Update ans ans = Math.min(ans, oneCount + zeroCount[i]); oneCount++; } } // If all 1s are deleted ans = Math.min(ans, oneCount); // Return the minimum // number of deletions return (ans == Number.MAX_VALUE) ? 0 : ans; } // Driver Code let str = "00101101" ; document.write( minimumDeletions(str)); </script> |
2
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.