Minimum swaps required to convert one binary string to another

Given two binary string M and N of equal length, the task is to find a minimum number of operations (swaps) required to convert string N to M.

Examples:

Input: str1 = "1101", str2 = "1110"
Output: 1
Swap last and second last element in the binary string, 
so that it become 1101

Input: str1 = "1110000", str2 = "0001101"
Output: 3


Approach: Initialize the counter and Iterate over the M such that if any non-equal elements found in both binary strings, increment the counter. In the end, if the counter is even then print the result/2 because for one swap two elements are non-identical.
Suppose S1 = “10” and S2 = “01”, so two pairs are non-identical, the count = 2 and as the count is even, so number of swaps are count/2, i.e. 1. Even count determines that there are chances to swap the elements.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Method to count swaps
void minSwaps(string str1, string str2)
{
    // Initialize the count
    int count = 0;
  
    // Iterate the loop with str1 length
    for (int i = 0; i < str1.length(); i++) {
  
        // If any non-equal elements are found
        // increment the counter
        if (str1[i] != str2[i])
            count++;
    }
  
    // If counter is even print the swap
    if (count % 2 == 0)
        cout << count / 2;
    else
        cout << "Not Possible";
}
  
// Driver code
int main()
{
    // Take two input
    string binaryString1 = "1110000";
    string binaryString2 = "0001101";
  
    // Call the method
    minSwaps(binaryString1, binaryString2);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to count minimum number of swap
// required to make string N to M
public class GFG {
  
    // Method to count swaps
    static void minSwaps(String str1, String str2)
    {
        // Initialize the count
        int count = 0;
  
        // Iterate the loop with str1 length
        for (int i = 0; i < str1.length(); i++) {
  
            // If any non-equal elements are found
            // increment the counter
            if (str1.charAt(i) != str2.charAt(i))
                count++;
        }
  
        // If counter is even print the swap
        if (count % 2 == 0)
            System.out.println(count / 2);
        else
            System.out.println("Not Possible");
    }
  
    // Driver Code
    public static void main(String args[])
    {
        // Take two input
        String binaryString1 = "1110000";
        String binaryString2 = "0001101";
  
        // Call the method
        minSwaps(binaryString1, binaryString2);
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of 
# the above approach 
  
# function to count swaps
def minSwaps(str1, str2) :
  
    # Initialize the count
    count = 0
  
    # Iterate the loop with 
    # length of str1
    for i in range(len(str1)) :
  
        # If any non-equal elements are 
        # found increment the counter 
        if str1[i] != str2[i] :
            count += 1
  
    # If counter is even print 
    # the swap 
    if count % 2 == 0 :
        print(count // 2)
    else :
        print("Not Possible")
  
  
# Driver code
if __name__ == "__main__" :
  
    # Take two input
    binaryString1 = "1110000"
    binaryString2 = "0001101"
  
    # Call the function
    minSwaps( binaryString1, binaryString2)
  
# This code is contributed by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to count minimum number of swap
// required to make string N to M
using System;
class GFG
{
  
// Method to count swaps
static void minSwaps(string str1, string str2)
{
    // Initialize the count
    int count = 0;
  
    // Iterate the loop with str1 length
    for (int i = 0; i < str1.Length; i++) {
  
        // If any non-equal elements are found
        // increment the counter
        if (str1[i] != str2[i])
            count++;
    }
  
    // If counter is even print the swap
    if (count % 2 == 0)
        Console.WriteLine(count / 2);
    else
        Console.WriteLine("Not Possible");
}
  
// Driver Code
public static void Main()
{
    // Take two input
    string binaryString1 = "1110000";
    string binaryString2 = "0001101";
  
    // Call the method
    minSwaps(binaryString1, binaryString2);
}
}
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)

chevron_right


PHP

Output:

3

Time Complexity: O(n)



My Personal Notes arrow_drop_up

सर्वशक्तिशाली इकलौता

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : Ryuga, Akanksha_Rai, Sach_Code