Minimum swaps to make two strings equal by swapping only with third string
Last Updated :
14 Jun, 2022
Given three strings A, B, and C of equal length, the task is to find the minimum number of swap operations that can be performed such that the strings A and B become equal when any swapping can be done only with C.
Examples:
Input: A = “xyz”, B = “yzx”, C = “yzx”
Output: 3
Explanation:
Swap all the characters of string C with string A one by one
Input: A = “pqr”, B = “stu”, C = “vwx”
Output: -1
Explanation
It’s impossible to make strings A and B equal, even by using C
Approach: Since the lengths of the strings are equal, the idea is to traverse the strings and check for the characters present in the strings.
For every index ‘i’, the following cases occur:
- If the character at the given index for the string A is the same as the character in B, then we can proceed to check the next character.
- If the character at the given index for the string A and B are not equal, then we check if the character at strings B and C or A and C are equal, and accordingly, we swap those characters.
- If none of the above conditions satisfies, then it’s impossible to make the strings A and B equal.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void swap( char & x, char & y)
{
char temp = x;
x = y;
y = temp;
}
void swapOperations(string a, string b, string c)
{
int l = a.length();
int i = 0;
int total_swaps = 0;
for (i = 0; i < l; i++) {
if (a[i] == b[i])
continue ;
if (a[i] == c[i]) {
swap(b[i], c[i]);
total_swaps++;
continue ;
}
if (b[i] == c[i]) {
swap(a[i], c[i]);
total_swaps++;
continue ;
}
break ;
}
if (i == l)
cout << total_swaps << endl;
else
cout << -1 << endl;
}
int main()
{
string a = "xyz" ;
string b = "yzx" ;
string c = "yzx" ;
swapOperations(a, b, c);
return 0;
}
|
Java
class GFG {
static void swapOperations( char []a, char []b, char []c)
{
int l = a.length;
int i = 0 ;
int total_swaps = 0 ;
char temp;
for (i = 0 ; i < l; i++) {
if (a[i] == b[i])
continue ;
if (a[i] == c[i]) {
temp = b[i];
b[i] = c[i];
c[i] = temp;
total_swaps++;
continue ;
}
if (b[i] == c[i]) {
temp = a[i];
a[i] = c[i];
c[i] = temp;
total_swaps++;
continue ;
}
break ;
}
if (i == l)
System.out.println(total_swaps) ;
else
System.out.println(- 1 ) ;
}
public static void main (String[] args)
{
String a = "xyz" ;
String b = "yzx" ;
String c = "yzx" ;
swapOperations(a.toCharArray(), b.toCharArray(), c.toCharArray());
}
}
|
Python3
def swapOperations(a, b, c) :
l = len (a);
i = 0 ;
total_swaps = 0 ;
for i in range (l) :
if (a[i] = = b[i]) :
continue ;
if (a[i] = = c[i]) :
b[i], c[i] = c[i], b[i];
total_swaps + = 1 ;
continue ;
if (b[i] = = c[i]) :
a[i], c[i] = c[i], a[i];
total_swaps + = 1 ;
continue ;
break ;
i + = 1 ;
if (i = = l) :
print (total_swaps) ;
else :
print ( - 1 );
if __name__ = = "__main__" :
a = "xyz" ;
b = "yzx" ;
c = "yzx" ;
swapOperations( list (a), list (b), list (c));
|
C#
using System;
class GFG {
static void swapOperations( char []a, char []b, char []c)
{
int l = a.Length;
int i = 0;
int total_swaps = 0;
char temp;
for (i = 0; i < l; i++) {
if (a[i] == b[i])
continue ;
if (a[i] == c[i]) {
temp = b[i];
b[i] = c[i];
c[i] = temp;
total_swaps++;
continue ;
}
if (b[i] == c[i]) {
temp = a[i];
a[i] = c[i];
c[i] = temp;
total_swaps++;
continue ;
}
break ;
}
if (i == l)
Console.WriteLine(total_swaps) ;
else
Console.WriteLine(-1) ;
}
public static void Main( string [] args)
{
string a = "xyz" ;
string b = "yzx" ;
string c = "yzx" ;
swapOperations(a.ToCharArray(), b.ToCharArray(), c.ToCharArray());
}
}
|
Javascript
<script>
function swapOperations(a, b, c) {
var l = a.length;
var i = 0;
var total_swaps = 0;
for (i = 0; i < l; i++) {
if (a[i] === b[i]) continue ;
if (a[i] === c[i]) {
var temp = b[i];
b[i] = c[i];
c[i] = temp;
total_swaps++;
continue ;
}
if (b[i] === c[i]) {
var temp = a[i];
a[i] = c[i];
c[i] = temp;
total_swaps++;
continue ;
}
break ;
}
if (i === l) document.write(total_swaps);
else document.write(-1);
}
var a = "xyz" ;
var b = "yzx" ;
var c = "yzx" ;
swapOperations(a.split( "" ), b.split( "" ), c.split( "" ));
</script>
|
Time Complexity: O(l), where l is the size of the given string a
Auxiliary Space: O(1), as no extra space is used
Share your thoughts in the comments
Please Login to comment...