Given a binary string S of 0s and 1s. The task is to make the given string a sequence of alternate characters by using the below operations:
- Remove some prefix from the start and append it to the end.
- Flip some or every bit in the given string.
Print the minimum number of bits to be flipped to make the given string alternating.
Input: S = “001”
No need to flip any element we can get alternating sequence by using left rotation: 010.
Input: S = “000001100”
Following steps to find minimum flips to get alternating string:
1. After rotating string 6 times towards left we will get: 100000001
2. Now we can apply flip operation as following: 101000001 -> 101010001 -> 101010101
Thus, minimum flips to make string alternating is 3.
Naive Approach: The naive approach is to take all N possible combinations and calculate the minimum number of bits To flip in each of those strings. Print the minimum count among all such combinations.
Time Complexity: O(N2), where N is the length of the string.
Auxiliary Space: O(N)
Efficient Approach: This can be solved by observing that the final string will be either of type “101010…” or “010101…” such that all 1s will either be at odd positions or at even positions. Follow the below steps to solve the problem:
- Create a prefix sum array where pref[i] means a number of changes required until index i.
- Create prefix arrays for both the above patterns.
- Check for every i, if substring[0, i] is appended at the end how many characters to be flipped required.
- Print the minimum number of flips among all the substrings in the above steps.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(N)
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 replacements to make the binary string alternating | Set 2
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimum swaps required to make a binary string alternating
- Number of flips to make binary string alternate | Set 1
- Left Rotation and Right Rotation of a String
- 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 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 non-adjacent pair flips required to remove all 0s from a Binary String
- Minimum flips to make all 1s in left and 0s in right | Set 2
- Min flips of continuous characters to make all characters same in a string
- Minimum digits to be removed to make either all digits or alternating digits same
- Minimum Subarray flips required to convert all elements of a Binary Array to K
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimize characters to be changed to make the left and right rotation of a string same
- Count minimum right flips to set all values in an array
- Find Bit whose minimum sequence flips makes all bits same
- Minimum flips required to generate continuous substrings of 0’s and 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 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.