Minimize removal of substring of 0s to remove all occurrences of 0s from a circular Binary String
Given circular binary string S of size N, the task is to count the minimum number of consecutive 0s required to be removed such that the string contains only 1s.
A circular string is a string whose first and last characters are considered to be adjacent to each other.
Examples:
Input: S = β11010001β
Output: 2
Explanation:
Remove the substring {S[2]}. Now, the string modifies to β1110001β.
Remove the substring {S[3], β¦, S[5]} of consecutive 0s. Now, the string modifies to β1111β.
Therefore, the minimum count of removals required is 2.Input: S = β00110000β
Output: 1
Approach: The idea to solve the given problem is to traverse the given string and count the number of substrings having the same number of 0s, say C. Now if the first and the last characters of the string are β0β, then print the value of (C β 1) as the minimum number of removals required. Otherwise, print the value of C as the result.
Note: If the given string contains all 0s, then the minimum number of removals required is 1. Consider this case separately.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count minimum number of // removal of consecutive 0s required to // make binary string consists only of 1s int minRemovals(string str, int N) { // Stores the count of removals int ans = 0; bool X = false ; // Traverse the string S for ( int i = 0; i < N; i++) { // If the current character is '0' if (str[i] == '0' ) { ans++; // Traverse until consecutive // characters are only '0's while (str[i] == '0' ) { i++; } } else { X = true ; } } // If the binary string only // contains 1s, then return 1 if (!X) return 1; // If the first and the last // characters are 0 if (str[0] == '0' and str[N - 1] == '0' ) { ans--; } // Return the resultant count return ans; } // Driver Code int main() { string S = "11010001" ; int N = S.size(); cout << minRemovals(S, N); return 0; } |
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG{ // Function to count minimum number of // removal of consecutive 0s required to // make binary string consists only of 1s static int minRemovals(String str, int N) { // Stores the count of removals int ans = 0 ; boolean X = false ; // Traverse the string S for ( int i = 0 ; i < N; i++) { // If the current character is '0' if (str.charAt(i) == '0' ) { ans++; // Traverse until consecutive // characters are only '0's while (i < N && str.charAt(i) == '0' ) { i++; } } else { X = true ; } } // If the binary string only // contains 1s, then return 1 if (!X) return 1 ; // If the first and the last // characters are 0 if (str.charAt( 0 ) == '0' && str.charAt(N - 1 ) == '0' ) { ans--; } // Return the resultant count return ans; } // Driver Code public static void main(String[] args) { String S = "11010001" ; int N = S.length(); System.out.println(minRemovals(S, N)); } } // This code is contributed by Kingash |
Python3
# Python3 program for the above approach # Function to count minimum number of # removal of consecutive 0s required to # make binary string consists only of 1s def minRemovals( str , N): # Stores the count of removals ans = 0 X = False # Traverse the string S i = 0 while i < N: # If the current character is '0' if ( str [i] = = '0' ): ans + = 1 # Traverse until consecutive # characters are only '0's while ( str [i] = = '0' ): i + = 1 else : X = True i + = 1 # If the binary string only # contains 1s, then return 1 if ( not X): return 1 # If the first and the last # characters are 0 if ( str [ 0 ] = = '0' and str [N - 1 ] = = '0' ): ans - = 1 # Return the resultant count return ans # Driver Code S = "11010001" N = len (S) print (minRemovals(S, N)) # This code is contributed by rohan07 |
C#
// C# program for the above approach using System; class GFG { // Function to count minimum number of // removal of consecutive 0s required to // make binary string consists only of 1s static int minRemovals( string str, int N) { // Stores the count of removals int ans = 0; bool X = false ; // Traverse the string S for ( int i = 0; i < N; i++) { // If the current character is '0' if (str[i] == '0' ) { ans++; // Traverse until consecutive // characters are only '0's while (str[i] == '0' ) { i++; } } else { X = true ; } } // If the binary string only // contains 1s, then return 1 if (!X) return 1; // If the first and the last // characters are 0 if (str[0] == '0' && str[N - 1] == '0' ) { ans--; } // Return the resultant count return ans; } // Driver Code public static void Main() { string S = "11010001" ; int N = S.Length; Console.Write(minRemovals(S, N)); } } // This code is contributed by subham348. |
Javascript
<script> // js program for the above approach // Function to count minimum number of // removal of consecutive 0s required to // make binary consists only of 1s function minRemovals(str, N) { // Stores the count of removals let ans = 0; let X = false ; // Traverse the S for (i = 0; i < N; i++) { // If the current character is '0' if (str[i] == '0' ) { ans++; // Traverse until consecutive // characters are only '0's while (str[i] == '0' ) { i++; } } else { X = true ; } } // If the binary only // contains 1s, then return 1 if (!X) return 1; // If the first and the last // characters are 0 if (str[0] == '0' && str[N - 1] == '0' ) { ans--; } // Return the resultant count return ans; } // Driver Code let S = "11010001" ; let N = S.length; document.write(minRemovals(S, N)); // This code is contributed by mohit kumar 29. </script> |
2
Time Complexity: O(N)
Auxiliary Space: O(1)