Minimum swaps required to convert one binary string to another
Last Updated :
28 Sep, 2022
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++
// 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;
}
Java
// 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);
}
}
Python 3
# 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
C#
// 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)
PHP
<?php
// PHP implementation of the above
// approach
// Method to count swaps
function minSwaps($str1, $str2)
{
// Initialize the count
$count = 0;
// Iterate the loop with str1 length
for ($i = 0; $i < strlen($str1); $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)
echo ($count / 2);
else
echo "Not Possible";
}
// Driver code
// Take two input
$binaryString1 = "1110000";
$binaryString2 = "0001101";
// Call the method
minSwaps($binaryString1, $binaryString2);
// This code is contributed
// by Sach_Code
?>
JavaScript
<script>
// JavaScript Program to count minimum number of swap
// required to make string N to M
// Method to count swaps
function minSwaps(str1, str2) {
// Initialize the count
var count = 0;
// Iterate the loop with str1 length
for (var 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) document.write(count / 2);
else document.write("Not Possible");
}
// Driver Code
// Take two input
var binaryString1 = "1110000";
var binaryString2 = "0001101";
// Call the method
minSwaps(binaryString1, binaryString2);
</script>
Time Complexity: O(n)
Auxiliary Space: O(1) it is using constant space for variables
Similar Reads
Minimum substring flips required to convert a Binary String to another Given two binary strings S1 and S2 of size N and M respectively, the task is to find the minimum number of reversal of substrings of equal characters required to convert the string S1 to S2. If it is not possible to convert the string S1 to S2, then print "-1". Examples: Input: S1 = "100001", S2 = "
6 min read
Minimum substring flips required to convert given binary string to another Given two binary strings A and B, the task is to find the minimum number of times a substring starting from the first character of A needs to be flipped, i.e. convert 1s to 0s and 0s to 1s, to convert A to B. Examples: Input: A = â0010â, B = â1011âOutput; 3Explanation:Step 1: Flip the entire string
7 min read
Minimum prefixes required to be flipped to convert a Binary String to another Given two binary strings A and B of length N, the task is to convert the string from A to string B by repeatedly flipping all the bits of a prefix of A, i.e. convert all the 0s in the prefix to 1s and vice-versa and print the minimum number of prefix flips required and the length of respective prefi
9 min read
Minimum operations required to convert a binary string to all 0s or all 1s Given a binary string str, the task is to find the minimum number of operations required to make all the characters of the string same i.e. either the resultant string contains all 0s or all 1s. In a single operation, any block of consecutive 0s can be converted to a block of consecutive 1s of the s
4 min read
Minimum given operations required to convert a given binary string to all 1's Given a binary number as a string str of length L. The task is to find the minimum number of operations needed so that the number becomes 2L-1, that is a string consisting of only 1's of the length L. In each operation, the number N can be replaced by N xor (N + 1). Examples: Input: str = "10010111"
7 min read