Minimize replacements to make any two of the three given strings equal

• Last Updated : 09 Dec, 2021

Given three strings A, B, and C of equal length, the task is to find the minimum number of replacement operations that can be performed such that any two out of the three strings become equal.

Examples:

Input: A = “aaa”, B = “bab”, C = “bbb”
Output:
Explanation: To make the strings B and C equal, B[1] = ‘a’ can be replaced with ‘b’. Hence the string B after replacement becomes B = “bbb” = C. Therefore, B and C can be made equal in 1 operation which is the minimum possible.

Input: A = “pqr”, B = “pqr”, C = “ppp”
Output:
Explanation: As A and B are already equal, no replacement operations are required.

Approach: The given problem can be solved by finding the operation count of all the three possible cases i.e, A = B or B = C or A = C, and taking the minimum of the three. The number of operations required to make strings X and Y equal can be calculated by traversing the strings and keeping track of the indices such that X[i] â‰  Y[i]. The count of such indices is the required number of operations.

Below is the implementation of the above approach:

C++

 `// C++ program of the above approach``#include ``using` `namespace` `std;` `// Function to find the minimum number of``// replacement to make two strings equal``int` `minimumReplacement(string X, string Y)``{``    ``// Stores the operation count``    ``int` `cnt = 0;``    ``for` `(``int` `i = 0; i < X.length(); i++)``        ``if` `(X[i] != Y[i])``            ``cnt++;` `    ``// Return Answer``    ``return` `cnt;``}` `// Function to find the minimum number of``// replacement to make any two strings``// out of the given three strings equal``int` `replacementOperations(``    ``string A, string B, string C)``{``    ``// Case where A and B will be equal``    ``int` `AB = minimumReplacement(A, B);` `    ``// Case where A and C will be equal``    ``int` `AC = minimumReplacement(A, C);` `    ``// Case where B and C will be equal``    ``int` `BC = minimumReplacement(B, C);` `    ``// Return the minimum of the three``    ``// above calculated values``    ``return` `min(AB, min(AC, BC));``}` `// Driver Code``int` `main()``{``    ``string A = ``"aaa"``;``    ``string B = ``"bab"``;``    ``string C = ``"bbb"``;` `    ``cout << replacementOperations(A, B, C);` `    ``return` `0;``}`

Java

 `// Java program of the above approach``import` `java.util.*;``public` `class` `GFG``{``  ` `// Function to find the minimum number of``// replacement to make two strings equal``static` `int` `minimumReplacement(String X, String Y)``{``  ` `    ``// Stores the operation count``    ``int` `cnt = ``0``;``    ``for` `(``int` `i = ``0``; i < X.length(); i++)``        ``if` `(X.charAt(i) != Y.charAt(i))``            ``cnt++;` `    ``// Return Answer``    ``return` `cnt;``}` `// Function to find the minimum number of``// replacement to make any two strings``// out of the given three strings equal``static` `int` `replacementOperations(``    ``String A, String B, String C)``{``    ``// Case where A and B will be equal``    ``int` `AB = minimumReplacement(A, B);` `    ``// Case where A and C will be equal``    ``int` `AC = minimumReplacement(A, C);` `    ``// Case where B and C will be equal``    ``int` `BC = minimumReplacement(B, C);` `    ``// Return the minimum of the three``    ``// above calculated values``    ``return` `Math.min(AB, Math.min(AC, BC));``}` `// Driver Code``public` `static` `void` `main(String args[])``{``    ``String A = ``"aaa"``;``    ``String B = ``"bab"``;``    ``String C = ``"bbb"``;` `    ``System.out.println(replacementOperations(A, B, C));``}``}` `// This code is contributed by Samim Hossain Mondal`

Python3

 `# Python program of the above approach` `# Function to find the minimum number of``# replacement to make two strings equal``def` `minimumReplacement(X, Y):``    ``# Stores the operation count``    ``cnt ``=` `0``    ``for` `i ``in` `range``(``len``(X)):``        ``if` `(X[i] !``=` `Y[i]):``            ``cnt ``+``=` `1` `    ``# Return Answer``    ``return` `cnt` `# Function to find the minimum number of``# replacement to make any two strings``# out of the given three strings equal``def` `replacementOperations(A, B, C):``  ` `    ``# Case where A and B will be equal``    ``AB ``=` `minimumReplacement(A, B)` `    ``# Case where A and C will be equal``    ``AC ``=` `minimumReplacement(A, C)` `    ``# Case where B and C will be equal``    ``BC ``=` `minimumReplacement(B, C)` `    ``# Return the minimum of the three``    ``# above calculated values``    ``return` `min``(AB, ``min``(AC, BC))` `# Driver Code``A ``=` `"aaa"``B ``=` `"bab"``C ``=` `"bbb"` `print``(replacementOperations(A, B, C))` `# This code is contributed by saurabh_jaiswal.`

C#

 `// C# program of the above approach``using` `System;``using` `System.Collections;` `class` `GFG``{``  ` `// Function to find the minimum number of``// replacement to make two strings equal``static` `int` `minimumReplacement(``string` `X, ``string` `Y)``{``  ` `    ``// Stores the operation count``    ``int` `cnt = 0;``    ``for` `(``int` `i = 0; i < X.Length; i++)``        ``if` `(X[i] != Y[i])``            ``cnt++;` `    ``// Return Answer``    ``return` `cnt;``}` `// Function to find the minimum number of``// replacement to make any two strings``// out of the given three strings equal``static` `int` `replacementOperations(``    ``string` `A, ``string` `B, ``string` `C)``{``  ` `    ``// Case where A and B will be equal``    ``int` `AB = minimumReplacement(A, B);` `    ``// Case where A and C will be equal``    ``int` `AC = minimumReplacement(A, C);` `    ``// Case where B and C will be equal``    ``int` `BC = minimumReplacement(B, C);` `    ``// Return the minimum of the three``    ``// above calculated values``    ``return` `Math.Min(AB, Math.Min(AC, BC));``}` `// Driver Code``public` `static` `void` `Main()``{``    ``string` `A = ``"aaa"``;``    ``string` `B = ``"bab"``;``    ``string` `C = ``"bbb"``;` `    ``Console.Write(replacementOperations(A, B, C));``}``}` `// This code is contributed by Samim Hossain Mondal`

Javascript

 ``
Output
`1`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up