Given a binary string, that is it contains only 0s and 1s. We need to make this string a sequence of alternate characters by flipping some of the bits, our goal is to minimize the number of bits to be flipped.
Input : str = “001” Output : 1 Minimum number of flips required = 1 We can flip 1st bit from 0 to 1 Input : str = “0001010111” Output : 2 Minimum number of flips required = 2 We can flip 2nd bit from 0 to 1 and 9th bit from 1 to 0 to make alternate string “0101010101”.
Expected time complexity : O(n) where n is length of input string.
We can solve this problem by considering all possible results, As we are supposed to get alternate string, there are only 2 possibilities, alternate string starting with 0 and alternate string starting with 1. We will try both cases and choose the string which will require minimum number of flips as our final answer.
Trying a case requires O(n) time in which we will loop over all characters of given string, if current character is expected character according to alternation then we will do nothing otherwise we will increase flip count by 1. After trying strings starting with 0 and starting with 1, we will choose the string with minimum flip count.
Total time complexity of solution will be O(n)
This article is contributed by Utkarsh Trivedi. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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 number of flips with rotation to make binary string alternating
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimum Group Flips to Make Binary Array Elements Same
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Minimum number of characters to be removed to make a binary string alternate
- Min flips of continuous characters to make all characters same in a string
- Minimum flips to make all 1s in left and 0s in right | Set 2
- 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 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
- Minimum substring flips required to convert given binary string to another
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Count minimum right flips to set all values in an array
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Rearrange a binary string as alternate x and y occurrences
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- Find Bit whose minimum sequence flips makes all bits same
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Minimize count of flips required such that no substring of 0s have length exceeding K