Skip to content
Related Articles

Related Articles

Minimum swaps required to make a binary string alternating
  • Difficulty Level : Hard
  • Last Updated : 28 Dec, 2018

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 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:

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));
    }
}

Python 3




# 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.
?>
Output:
1



My Personal Notes arrow_drop_up
Recommended Articles
Page :