Minimum swaps required to make a binary string alternating
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.
Examples:
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 the first position. We consider two cases and find a 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 the 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:
C++
// CPP implementation of the approach #include<bits/stdc++.h> using namespace std; // returns the minimum number of swaps // of a binary string // passed as the argument // to make it alternating int countMinSwaps(string st) { int min_swaps = 0; // counts number of zeroes at odd // and even positions int odd_0 = 0, even_0 = 0; // counts number of ones at odd // and even positions int odd_1 = 0, even_1 = 0; int n = st.length(); for ( int i = 0; i < n; i++) { if (i % 2 == 0) { if (st[i] == '1' ) even_1++; else even_0++; } else { if (st[i] == '1' ) odd_1++; else odd_0++; } } // alternating string starts with 0 int cnt_swaps_1 = min(even_0, odd_1); // alternating string starts with 1 int cnt_swaps_2 = min(even_1, odd_0); // calculates the minimum number of swaps return min(cnt_swaps_1, cnt_swaps_2); } // Driver code int main() { string st = "000111" ; cout<<countMinSwaps(st)<<endl; return 0; } // This code is contributed by Surendra_Gangwar |
Java
// Java implementation of the approach class GFG { // returns the minimum number of swaps // of a binary string // passed as the argument // to make it alternating static int countMinSwaps(String st) { int min_swaps = 0 ; // counts number of zeroes at odd // and even positions int odd_0 = 0 , even_0 = 0 ; // counts number of ones at odd // and even positions int odd_1 = 0 , even_1 = 0 ; int n = st.length(); for ( int i = 0 ; i < n; i++) { if (i % 2 == 0 ) { if (st.charAt(i) == '1' ) even_1++; else even_0++; } else { if (st.charAt(i) == '1' ) odd_1++; else odd_0++; } } // alternating string starts with 0 int cnt_swaps_1 = Math.min(even_0, odd_1); // alternating string starts with 1 int cnt_swaps_2 = Math.min(even_1, odd_0); // calculates the minimum number of swaps return Math.min(cnt_swaps_1, cnt_swaps_2); } // Driver code public static void main(String[] args) { String st = "000111" ; System.out.println(countMinSwaps(st)); } } |
Python3
# Python3 implementation of the # above approach # returns the minimum number of swaps # of a binary string # passed as the argument # to make it alternating def countMinSwaps(st) : min_swaps = 0 # counts number of zeroes at odd # and even positions odd_0, even_0 = 0 , 0 # counts number of ones at odd # and even positions odd_1, even_1 = 0 , 0 n = len (st) for i in range ( 0 , n) : if i % 2 = = 0 : if st[i] = = "1" : even_1 + = 1 else : even_0 + = 1 else : if st[i] = = "1" : odd_1 + = 1 else : odd_0 + = 1 # alternating string starts with 0 cnt_swaps_1 = min (even_0, odd_1) # alternating string starts with 1 cnt_swaps_2 = min (even_1, odd_0) # calculates the minimum number of swaps return min (cnt_swaps_1, cnt_swaps_2) # Driver code if __name__ = = "__main__" : st = "000111" # Function call print (countMinSwaps(st)) # This code is contributed by # ANKITRAI1 |
C#
// C# implementation of the approach using System; public class GFG { // returns the minimum number of swaps // of a binary string // passed as the argument // to make it alternating public static int countMinSwaps( string st) { int min_swaps = 0; // counts number of zeroes at odd // and even positions int odd_0 = 0, even_0 = 0; // counts number of ones at odd // and even positions int odd_1 = 0, even_1 = 0; int n = st.Length; for ( int i = 0; i < n; i++) { if (i % 2 == 0) { if (st[i] == '1' ) { even_1++; } else { even_0++; } } else { if (st[i] == '1' ) { odd_1++; } else { odd_0++; } } } // alternating string starts with 0 int cnt_swaps_1 = Math.Min(even_0, odd_1); // alternating string starts with 1 int cnt_swaps_2 = Math.Min(even_1, odd_0); // calculates the minimum number of swaps return Math.Min(cnt_swaps_1, cnt_swaps_2); } // Driver code public static void Main( string [] args) { string st = "000111" ; Console.WriteLine(countMinSwaps(st)); } } // This code is contributed by Shrikant13 |
PHP
<?php // PHP implementation of the approach // returns the minimum number of swaps // of a binary string passed as the // argument to make it alternating function countMinSwaps( $st ) { $min_swaps = 0; // counts number of zeroes at odd // and even positions $odd_0 = 0; $even_0 = 0; // counts number of ones at odd // and even positions $odd_1 = 0; $even_1 = 0; $n = strlen ( $st ); for ( $i = 0; $i < $n ; $i ++) { if ( $i % 2 == 0) { if ( $st [ $i ] == '1' ) { $even_1 ++; } else { $even_0 ++; } } else { if ( $st [ $i ] == '1' ) { $odd_1 ++; } else { $odd_0 ++; } } } // alternating string starts with 0 $cnt_swaps_1 = min( $even_0 , $odd_1 ); // alternating string starts with 1 $cnt_swaps_2 = min( $even_1 , $odd_0 ); // calculates the minimum number of swaps return min( $cnt_swaps_1 , $cnt_swaps_2 ); } // Driver code $st = "000111" ; echo (countMinSwaps( $st )); // This code is contributed by Sachin. ?> |
Javascript
<script> // Javascript implementation of the approach // returns the minimum number of swaps // of a binary string // passed as the argument // to make it alternating function countMinSwaps(st) { let min_swaps = 0; // counts number of zeroes at odd // and even positions let odd_0 = 0, even_0 = 0; // counts number of ones at odd // and even positions let odd_1 = 0, even_1 = 0; let n = st.length; for (let i = 0; i < n; i++) { if (i % 2 == 0) { if (st[i] == '1' ) { even_1++; } else { even_0++; } } else { if (st[i] == '1' ) { odd_1++; } else { odd_0++; } } } // alternating string starts with 0 let cnt_swaps_1 = Math.min(even_0, odd_1); // alternating string starts with 1 let cnt_swaps_2 = Math.min(even_1, odd_0); // calculates the minimum number of swaps return Math.min(cnt_swaps_1, cnt_swaps_2); } let st = "000111" ; document.write(countMinSwaps(st)); // This code is contributed by divyesh072019. </script> |
1
Time Complexity: O(n) // n is the length of the string
Space Complexity: O(1) //since there is no extra array used so constant space is used
Approach for odd and even length string :
Let string length be N.
In this approach, we will consider three cases :
1. Answer is impossible when total number of ones > total number of zeroes + 1 or total number of zeroes > total number of ones + 1.
2. String is of even length:
We will count number of ones on odd positions ( one_odd ) and number of ones on even positions ( one_even ) then answer is min ( N/2 – one_odd , N/2 – one_even )
3. String is of odd length :
Here we consider two cases :
i) total number of ones > total number of zeroes (then we have put ones on even positions) so, answer is ( (N + 1) / 2 – number of ones at even positions).
ii) total number of zeroes > total number of ones (then we have put zeroes on even positions) so, answer is ( (N + 1) / 2 – number of zeroes at even positions ).
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // function to count minimum swaps // required to make binary string // alternating int countMinSwaps(string s) { int N = s.size(); // stores total number of ones int one = 0; // stores total number of zeroes int zero = 0; for ( int i = 0; i < N; i++) { if (s[i] == '1' ) one++; else zero++; } // checking impossible condition if (one > zero + 1 || zero > one + 1) return -1; // odd length string if (N % 2) { // number of even positions int num = (N + 1) / 2; // stores number of zeroes and // ones at even positions int one_even = 0, zero_even = 0; for ( int i = 0; i < N; i++) { if (i % 2 == 0) { if (s[i] == '1' ) one_even++; else zero_even++; } } if (one > zero) return num - one_even; else return num - zero_even; } // even length string else { // stores number of ones at odd // and even position respectively int one_odd = 0, one_even = 0; for ( int i = 0; i < N; i++) { if (s[i] == '1' ) { if (i % 2) one_odd++; else one_even++; } } return min(N / 2 - one_odd, N / 2 - one_even); } } // Driver code int main() { string s = "111000" ; cout << countMinSwaps(s); return 0; } |
Java
// Java implementation of the above approach import java.util.*; class GFG{ // function to count minimum swaps // required to make binary String // alternating static int countMinSwaps(String s) { int N = s.length(); // stores total number of ones int one = 0 ; // stores total number of zeroes int zero = 0 ; for ( int i = 0 ; i < N; i++) { if (s.charAt(i) == '1' ) one++; else zero++; } // checking impossible condition if (one > zero + 1 || zero > one + 1 ) return - 1 ; // odd length String if (N % 2 == 1 ) { // number of even positions int num = (N + 1 ) / 2 ; // stores number of zeroes and // ones at even positions int one_even = 0 , zero_even = 0 ; for ( int i = 0 ; i < N; i++) { if (i % 2 == 0 ) { if (s.charAt(i) == '1' ) one_even++; else zero_even++; } } if (one > zero) return num - one_even; else return num - zero_even; } // even length String else { // stores number of ones at odd // and even position respectively int one_odd = 0 , one_even = 0 ; for ( int i = 0 ; i < N; i++) { if (s.charAt(i) == '1' ) { if (i % 2 == 1 ) one_odd++; else one_even++; } } return Math.min(N / 2 - one_odd, N / 2 - one_even); } } // Driver code public static void main(String[] args) { String s = "111000" ; System.out.print(countMinSwaps(s)); } } // This code is contributed by gauravrajput1 |
Python3
# Python implementation of the above approach # function to count minimum swaps # required to make binary string # alternating def countMinSwaps(s): N = len (s) # stores total number of ones one = 0 # stores total number of zeroes zero = 0 for i in range (N): if (s[i] = = '1' ): one + = 1 else : zero + = 1 # checking impossible condition if (one > zero + 1 or zero > one + 1 ): return - 1 # odd length string if (N % 2 ): # number of even positions num = (N + 1 ) / 2 # stores number of zeroes and # ones at even positions one_even = 0 zero_even = 0 for i in range (N): if (i % 2 = = 0 ): if (s[i] = = '1' ): one_even + = 1 else : zero_even + = 1 if (one > zero): return num - one_even else : return num - zero_even # even length string else : # stores number of ones at odd # and even position respectively one_odd = 0 one_even = 0 for i in range (N): if (s[i] = = '1' ): if (i % 2 ): one_odd + = 1 else : one_even + = 1 return min (N / / 2 - one_odd, N / / 2 - one_even) # Driver code s = "111000" print (countMinSwaps(s)) # This code is contributed by shivanisinghss2110 |
C#
// C# implementation of the above approach using System; class GFG{ // Function to count minimum swaps // required to make binary String // alternating static int countMinSwaps( string s) { int N = s.Length; // Stores total number of ones int one = 0; // Stores total number of zeroes int zero = 0; for ( int i = 0; i < N; i++) { if (s[i] == '1' ) one++; else zero++; } // Checking impossible condition if (one > zero + 1 || zero > one + 1) return -1; // Odd length String if (N % 2 == 1) { // Number of even positions int num = (N + 1) / 2; // Stores number of zeroes and // ones at even positions int one_even = 0, zero_even = 0; for ( int i = 0; i < N; i++) { if (i % 2 == 0) { if (s[i] == '1' ) one_even++; else zero_even++; } } if (one > zero) return num - one_even; else return num - zero_even; } // Even length String else { // Stores number of ones at odd // and even position respectively int one_odd = 0, one_even = 0; for ( int i = 0; i < N; i++) { if (s[i] == '1' ) { if (i % 2 == 1) one_odd++; else one_even++; } } return Math.Min(N / 2 - one_odd, N / 2 - one_even); } } // Driver code public static void Main(String[] args) { string s = "111000" ; Console.Write(countMinSwaps(s)); } } // This code is contributed by shivanisinghss2110 |
Javascript
<script> // JavaScript implementation of the above approach // function to count minimum swaps // required to make binary String // alternating function countMinSwaps(s) { var N = s.length; // stores total number of ones var one = 0; // stores total number of zeroes var zero = 0; for ( var i = 0; i < N; i++) { if (s.charAt(i) == '1' ) one++; else zero++; } // checking impossible condition if (one > zero + 1 || zero > one + 1) return -1; // odd length String if (N % 2 == 1) { // number of even positions var num = (N + 1) / 2; // stores number of zeroes and // ones at even positions var one_even = 0, zero_even = 0; for ( var i = 0; i < N; i++) { if (i % 2 == 0) { if (s.charAt(i) == '1' ) one_even++; else zero_even++; } } if (one > zero) return num - one_even; else return num - zero_even; } // even length String else { // stores number of ones at odd // and even position respectively var one_odd = 0, one_even = 0; for ( var i = 0; i < N; i++) { if (s.charAt(i) == '1' ) { if (i % 2 == 1) one_odd++; else one_even++; } } return Math.min(N / 2 - one_odd, N / 2 - one_even); } } // Driver code var s = "111000" ; document.write(countMinSwaps(s)); // This code is contributed by shivanisinghss2110 </script> |
1
Time Complexity: O(N)
Auxiliary Space: O(1)