Given a binary string S of length N, the task is to find the minimum number of bit flips required to convert the given string such that it contains only continuous substrings of 0’s and 1’s such that the final string is in the form of 000..000, 111..111, 111…000 or 000…111.
Input: S = 000100101, N = 9
000100101 -> 000000001
Input: S = 01100, N = 5
01100 -> 11100
The minimum number of flips can be calculated efficiently in two linear traversals.
In the first traversal, we will calculate what can be the minimum number of flips required in the worst case, as it can be equal to the number of total 0’s initially.
In the second traversal, at every step, the total number of flip required will be the sum of total 1’s before that point and total 0’s after that point. we will take a minimum of all values calculated at every step.
Hence, to solve the problem, follow the steps below:
- Initialize variables count0 = 0, count1 = 0 and res = 0. where, count0 strores count of 0 and count1 stores count of 1 and res stores the bit flips required.
- Traverse the input string, calculate 0’s and store it in res variable.
- Traverse the input string and subtract the count of 0 if character 0 is found and store the count of character 1 in variable count1 and updates the res as min(res, count0+count1).
Below is the implementation of the above approach.
Time complexity: O(k), where, k is length of binary string.
Space complexity: O(1)
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Min flips of continuous characters to make all characters same in a string
- Minimum flips required to keep all 1s together in a Binary string
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimum Subarray flips required to convert all elements of a Binary Array to K
- Minimize count of flips required such that no substring of 0s have length exceeding K
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Minimum flips to make all 1s in left and 0s in right | Set 2
- Minimum Cost required to generate a balanced Bracket Sequence
- Count minimum right flips to set all values in an array
- Find Bit whose minimum sequence flips makes all bits same
- Minimum Group Flips to Make Binary Array Elements Same
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Minimum number of flips with rotation to make binary string alternating
- Generate a String of having N*N distinct non-palindromic Substrings
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Minimize splits to generate monotonous Substrings from given String
- Minimum letters to be removed to make all occurrences of a given letter continuous
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.