Given a binary string str, the task is to find the minimum number of flips required to keep all 1s together in the given binary string, i.e. there must not be any 0 between 1s in the string.
Input: str = “0011111100”
Explanation: We dont need to flip any bits because all the ones are grouped together and there is no zero between any two ones.
Input: str = “11100111000101”
Explanation: We can flip the 4th and 5th bit to make them 1 and flip 12th and 14th bit to make them 0. So the resulting string is “11111111000000” with 4 possible flips.
Approach: To solve the problem mentioned above we will implement the dynamic programming approach where we will have the following states:
- First state is dp[i] which signifies the number of flips required to make all zeroes upto the ith bit.
- Second state dp[i] which signifies the number of flips required to make the current bit 1 such that the conditions given in the question is satisfied.
So the required answer will be minimum flips for making the current bit 1 + minimum flips for making all bits after the current bit 0 for all values of i. But if all the bits in the given string is 0 then we don’t have to change anything so we can check the minimum between our answer and number of flips required to make the string with all zeroes. So we can compute the answer by iterating over all the characters in the string where,
answer = min ( answer, dp[i] + dp[n-1] – dp[i])
dp[i] = Minimum number of flips to set current bit to 1
dp[n-1] – dp[i] = Minimum number of flips required to make all bits after i as 0
Below is the implementation of above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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 flips required to generate continuous substrings of 0’s and 1’s
- Minimum flips required to maximize a number with k set bits
- Minimum swaps required to convert one binary string to another
- Minimum given operations required to convert a given binary string to all 1's
- Minimum number of operations required to sum to binary string S
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum swaps required to make a binary string alternating
- Minimum Group Flips to Make Binary Array Elements Same
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Number of flips to make binary string alternate | Set 1
- Minimum number of given operations required to convert a string to another string
- Minimum changes required to make first string substring of second string
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Minimum adjacent swaps required to Sort Binary array
- Minimum rotations required to get the same String | Set-2
- Minimum rotations required to get the same string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.