Given a binary string str of length N and an integer K where K is in the range (1 ≤ K ≤ N), the task is to find the minimum number of flips( conversion of 0s to 1 or vice versa) required to be performed on the given string such that the resulting string does not contain K or more zeros together.
Input: str = “11100000011”, K = 3
Flipping 6th and 7th characters modifies the string to “11100110011”.
Therefore, no substring of 0s are present in the string having length 3 or more.
Input: str = “110011”, K = 1
Flip 3rd and 4th characters then str = “111111” which does not contain 1 or more zeros together.
Naive Approach: The simplest approach is to generate all possible substrings of the given string and for each substring, check if it consists of only 0s or not. If found to be true, check if its length exceeds K or not. If found to be true, count the number of flips required for that substring. Finally, print the count of flips obtained.
Time Complexity: O(2N)
Auxiliary Space: O(N)
Efficient Approach: To make the count of contiguous zeros in resultant string less than K, choose the substrings consisting of only zeros of length ≥ K. So the problem reduces to finding the minimum flips in each substring. The final result will be the sum of minimum flips of all such subsegments. Below are the steps:
- Initialize the result to 0and the count of contiguous zeros (say cnt_zeros) to 0.
- Transverse the string and on encountering a ‘0’ increment cnt_zeros by 1.
- If cnt_zeros becomes equal to K then increment result and set cnt_zeros to 0.
- And when a ‘1’ comes then set cnt_zeros to 0 since the count of contiguous zeros should become 0.
Below the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum substring flips required to convert given binary string to another
- Minimize count of Subsets with difference between maximum and minimum element not exceeding K
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count number of triangles possible with length of sides not exceeding N
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum Subarray flips required to convert all elements of a Binary Array to K
- Minimum non-adjacent pair flips required to remove all 0s from a Binary String
- Minimum flips required to keep all 1s together in a Binary string
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Minimum length of substring whose rotation generates a palindromic substring
- Maximize subsequences having array elements not exceeding length of the subsequence
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.