Minimum swaps to make two strings equal by swapping only with third string

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

  1. 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.
  2. If the character at the given index for the string A and B are not equal, then we check if the character at string B and C or A and C are equal and accordingly we swap those characters.
  3. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the
// minimum number of swaps to make
// two strings equal
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to swap the characters
void swap(char& x, char& y)
{
    char temp = x;
    x = y;
    y = temp;
}
  
// Function to find the minimum number of swaps
// to make two strings equal
void swapOperations(string a, string b, string c)
{
    // Counting length of string
    int l = a.length();
    int i = 0;
  
    // Initializing the answer
    int total_swaps = 0;
  
    // For loop to iterate through the
    // given strings
    for (i = 0; i < l; i++) {
  
        // Condition if both character of
        // string a and b are equal
        if (a[i] == b[i])
            continue;
  
        // Condition if character of
        // string a and c are equal
        if (a[i] == c[i]) {
  
            // If yes, then swap
            // the characters
            swap(b[i], c[i]);
            total_swaps++;
            continue;
        }
  
        // Condition if character of
        // string b and c are equal
        if (b[i] == c[i]) {
  
            // If yes, then swap
            // the characters
            swap(a[i], c[i]);
            total_swaps++;
            continue;
        }
  
        // Else, it is impossible to make
        // both the strings equal
        break;
    }
  
    // Printing the answer
    if (i == l)
        cout << total_swaps << endl;
    else
        cout << -1 << endl;
}
  
// Driver Code
int main()
{
    string a = "xyz";
    string b = "yzx";
    string c = "yzx";
  
    swapOperations(a, b, c);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find the 
// minimum number of swaps to make 
// two strings equal 
class GFG {
      
    // Function to find the minimum number of swaps 
    // to make two strings equal 
    static void swapOperations(char []a, char []b, char []c) 
    
        // Counting length of string 
        int l = a.length; 
        int i = 0
      
        // Initializing the answer 
        int total_swaps = 0
        char temp;
      
        // For loop to iterate through the 
        // given strings 
        for (i = 0; i < l; i++) { 
      
            // Condition if both character of 
            // string a and b are equal 
            if (a[i] == b[i]) 
                continue
      
            // Condition if character of 
            // string a and c are equal 
            if (a[i] == c[i]) { 
      
                // If yes, then swap 
                // the characters 
                //    swap(b[i], c[i]); 
                temp = b[i];
                b[i] = c[i];
                c[i] = temp;
                  
                total_swaps++; 
                continue
            
      
            // Condition if character of 
            // string b and c are equal 
            if (b[i] == c[i]) { 
      
                // If yes, then swap 
                // the characters 
                //swap(a[i], c[i]); 
                temp = a[i];
                a[i] = c[i];
                c[i] = temp;
                  
                total_swaps++; 
                continue
            
      
            // Else, it is impossible to make 
            // both the strings equal 
            break
        
      
        // Printing the answer 
        if (i == l) 
                System.out.println(total_swaps) ; 
        else
                System.out.println(-1) ; 
    
      
    // Driver Code 
    public static void main (String[] args)
    
        String a = "xyz"
        String b = "yzx"
        String c = "yzx"
      
        swapOperations(a.toCharArray(), b.toCharArray(), c.toCharArray()); 
      
    
}
  
// This code is contributed by Yash_R

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to find the 
# minimum number of swaps to make 
# two strings equal 
  
# Function to find the minimum number of swaps 
# to make two strings equal 
def swapOperations(a, b, c) : 
  
    # Counting length of string 
    l = len(a); 
    i = 0
  
    # Initializing the answer 
    total_swaps = 0
  
    # For loop to iterate through the 
    # given strings 
    for i in range(l) :
  
        # Condition if both character of 
        # string a and b are equal 
        if (a[i] == b[i]) :
            continue
  
        # Condition if character of 
        # string a and c are equal 
        if (a[i] == c[i]) :
  
            # If yes, then swap 
            # the characters 
            #swap(b[i], c[i]); 
            b[i], c[i] = c[i], b[i];
            total_swaps += 1
            continue
  
        # Condition if character of 
        # string b and c are equal 
        if (b[i] == c[i]) :
  
            # If yes, then swap 
            # the characters 
            # swap(a[i], c[i]); 
            a[i], c[i] = c[i], a[i];
            total_swaps += 1
            continue
  
        # Else, it is impossible to make 
        # both the strings equal 
        break
      
    i += 1;
  
    # Printing the answer 
    if (i == l) :
        print(total_swaps) ; 
    else :
        print(-1); 
  
# Driver Code 
if __name__ == "__main__"
  
    a = "xyz"
    b = "yzx"
    c = "yzx"
  
    swapOperations(list(a), list(b), list(c)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to find the 
// minimum number of swaps to make 
// two strings equal
using System;
  
class GFG {
      
    // Function to find the minimum number of swaps 
    // to make two strings equal 
    static void swapOperations(char []a, char []b, char []c) 
    
        // Counting length of string 
        int l = a.Length; 
        int i = 0; 
      
        // Initializing the answer 
        int total_swaps = 0; 
        char temp;
      
        // For loop to iterate through the 
        // given strings 
        for (i = 0; i < l; i++) { 
      
            // Condition if both character of 
            // string a and b are equal 
            if (a[i] == b[i]) 
                continue
      
            // Condition if character of 
            // string a and c are equal 
            if (a[i] == c[i]) { 
      
                // If yes, then swap 
                // the characters 
                //    swap(b[i], c[i]); 
                temp = b[i];
                b[i] = c[i];
                c[i] = temp;
                  
                total_swaps++; 
                continue
            
      
            // Condition if character of 
            // string b and c are equal 
            if (b[i] == c[i]) { 
      
                // If yes, then swap 
                // the characters 
                //swap(a[i], c[i]); 
                temp = a[i];
                a[i] = c[i];
                c[i] = temp;
                  
                total_swaps++; 
                continue
            
      
            // Else, it is impossible to make 
            // both the strings equal 
            break
        
      
        // Printing the answer 
        if (i == l) 
                Console.WriteLine(total_swaps) ; 
        else
                Console.WriteLine(-1) ; 
    
      
    // Driver Code 
    public static void Main(string[] args)
    
        string a = "xyz"
        string b = "yzx"
        string c = "yzx"
      
        swapOperations(a.ToCharArray(), b.ToCharArray(), c.ToCharArray()); 
      
    
}
  
// This code is contributed by Yash_R

chevron_right


Output:

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 : AnkitRai01, Yash_R

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.