You are given a binary string of even length and equal number of 0’s and 1’s. What is the minimum number of swaps to make the string alternating. A binary string is alternating if no two consecutive elements are equal.
Input : 000111 Output : 1 Explanation : Swap index 2 and index 5 to get 010101 Input : 1010 Output : 0
We may either get a 1 at first position or a zero at first position. We consider two cases and find the minimum of two cases. Note that it is given that there are equal of numbers of 1’s and 0’s in the string and string is of even length.
1. Count number of zeroes at odd position and even position of the string. Let their count be odd_0 and even_0 respectively.
2. Count number of ones at odd position and even position of the string. Let their count be odd_1 and even_1 respectively.
3. We will always swap a 1 with a 0 (never a 1 with a 1 or a 0 with a 0). So we just check if our alternating string starts with 0 then the number of swaps is min(even_0, odd_1) and if our alternating string starts with 1 then the number of swaps is min(even_1, odd_0). The answer is min of these two .
Below is the implementation of above approach:
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 swaps required to make a binary string divisible by 2^k
- Minimum number of swaps required to make the string K periodic
- Minimum swaps required to convert one binary string to another
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum number of swaps to make two binary string equal
- Minimum adjacent swaps required to Sort Binary array
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Minimum swaps to make two strings equal by swapping only with third string
- Minimum changes required to make first string substring of second string
- Minimum operations required to make the string satisfy the given condition
- Minimum flips required to keep all 1s together in a Binary string
- Minimum given operations required to convert a given binary string to all 1'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 number of operations required to sum to binary string S
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum number of operations required to obtain a given Binary String
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum size binary string required such that probability of deleting two 1's at random is 1/X
- Minimum number of characters to be removed to make a binary string alternate
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.