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.
Input: A = “xyz”, B = “yzx”, C = “yzx”
Swap all the characters of string C with string A one by one
Input: A = “pqr”, B = “stu”, C = “vwx”
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:
- 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 string 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:
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.
- Minimum number of swaps to make two binary string equal
- Minimum number of swaps required to make the string K periodic
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum swaps required to make a binary string alternating
- Minimum move to end operations to make all strings equal
- Minimum characters to be deleted from the end to make given two strings equal
- Minimum characters to be deleted from the beginning of two strings to make them equal
- Minimum number of given operations required to make two strings equal
- Minimum number of operations required to make two strings equal
- Find the minimum number of preprocess moves required to make two strings equal
- Check if two strings can be made equal by swapping one character among each other
- Minimum swaps required to convert one binary string to another
- Number of character corrections in the given strings to make them equal
- Minimum number of adjacent swaps to convert a string into its given anagram
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Minimum cost to make two strings same
- Minimum Cost To Make Two Strings Identical
- Minimum number of pairs required to make two strings same
- Minimum Cost to make two Numeric Strings Identical
- Minimum operations to make frequency of all characters equal K