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++
#include <bits/stdc++.h>
using namespace std;
void minSwaps(string str1, string str2)
{
int count = 0;
for ( int i = 0; i < str1.length(); i++) {
if (str1[i] != str2[i])
count++;
}
if (count % 2 == 0)
cout << count / 2;
else
cout << "Not Possible" ;
}
int main()
{
string binaryString1 = "1110000" ;
string binaryString2 = "0001101" ;
minSwaps(binaryString1, binaryString2);
return 0;
}
|
Java
public class GFG {
static void minSwaps(String str1, String str2)
{
int count = 0 ;
for ( int i = 0 ; i < str1.length(); i++) {
if (str1.charAt(i) != str2.charAt(i))
count++;
}
if (count % 2 == 0 )
System.out.println(count / 2 );
else
System.out.println( "Not Possible" );
}
public static void main(String args[])
{
String binaryString1 = "1110000" ;
String binaryString2 = "0001101" ;
minSwaps(binaryString1, binaryString2);
}
}
|
Python 3
def minSwaps(str1, str2) :
count = 0
for i in range ( len (str1)) :
if str1[i] ! = str2[i] :
count + = 1
if count % 2 = = 0 :
print (count / / 2 )
else :
print ( "Not Possible" )
if __name__ = = "__main__" :
binaryString1 = "1110000"
binaryString2 = "0001101"
minSwaps( binaryString1, binaryString2)
|
C#
using System;
class GFG
{
static void minSwaps( string str1, string str2)
{
int count = 0;
for ( int i = 0; i < str1.Length; i++) {
if (str1[i] != str2[i])
count++;
}
if (count % 2 == 0)
Console.WriteLine(count / 2);
else
Console.WriteLine( "Not Possible" );
}
public static void Main()
{
string binaryString1 = "1110000" ;
string binaryString2 = "0001101" ;
minSwaps(binaryString1, binaryString2);
}
}
|
PHP
<?php
function minSwaps( $str1 , $str2 )
{
$count = 0;
for ( $i = 0; $i < strlen ( $str1 ); $i ++)
{
if ( $str1 [ $i ] != $str2 [ $i ])
$count ++;
}
if ( $count % 2 == 0)
echo ( $count / 2);
else
echo "Not Possible" ;
}
$binaryString1 = "1110000" ;
$binaryString2 = "0001101" ;
minSwaps( $binaryString1 , $binaryString2 );
?>
|
Javascript
<script>
function minSwaps(str1, str2) {
var count = 0;
for ( var i = 0; i < str1.length; i++) {
if (str1[i] !== str2[i]) count++;
}
if (count % 2 === 0) document.write(count / 2);
else document.write( "Not Possible" );
}
var binaryString1 = "1110000" ;
var binaryString2 = "0001101" ;
minSwaps(binaryString1, binaryString2);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1) it is using constant space for variables
Last Updated :
28 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...