Given a binary string S, the task is to find the minimum number of operations of flipping at most two non-adjacent characters from the binary string required to remove all 0s.
Input: S = “110010”
Step 1: Flip indices 2 and 5. The string is modified to “111011”
Step 2: Flip only index 3. The string is modified to “111111”.
Therefore, minimum operations required is 2.
Input: S= “110”
Naive Approach: The simplest approach is to traverse the given string. For all characters of the string found to be ‘0’, traverse its right to find the next ‘0‘ which is not adjacent to the current character. Flip both the characters and increment answer by 1. If no ‘0’ to the right, flip the current character and increment the answer by 1.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The idea is to store the index of previous characters those need to be flipped. Below is the illustration with the help of steps:
- Initialize two variables to maintain the index of previously found 0s in the string with -1.
- Traverse the string and check if the last two found indices are not adjacent, then increment the counter by 1. Also, update the position of the last two previously found indices.
- Finally, after completing the traversal, increment the counter by 2 if both the last found indices are not -1. Otherwise, increment the counter by 1 if one of the last found indices is not -1.
Below is 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
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum flips required to keep all 1s together in a Binary string
- Minimum Subarray flips required to convert all elements of a Binary Array to K
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimum substring flips required to convert given binary string to another
- 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 convert given string into concatenation of equal substrings of length K
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Minimum number of flips with rotation to make binary string alternating
- Minimize count of flips required such that no substring of 0s have length exceeding K
- Minimum operations required to convert a binary string to all 0s or all 1s
- Number of flips to make binary string alternate | Set 1
- Find Bit whose minimum sequence flips makes all bits same
- Minimum flips to make all 1s in right and 0s in left
- Min flips of continuous characters to make all characters same in a string
- Minimum given operations required to convert a given binary string to all 1's
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum shifts of substrings of 1s required to group all 1s together in a given Binary string
- Minimum bit flips such that every K consecutive bits contain at least one set bit
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.