Minimize replacements to make any two of the three given strings equal
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: 1
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: 0
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 <bits/stdc++.h> 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
<script> // Javascript program of the above approach // Function to find the minimum number of // replacement to make two strings equal function minimumReplacement(X, Y) { // Stores the operation count let cnt = 0; for (let 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 function replacementOperations(A, B, C) { // Case where A and B will be equal let AB = minimumReplacement(A, B); // Case where A and C will be equal let AC = minimumReplacement(A, C); // Case where B and C will be equal let BC = minimumReplacement(B, C); // Return the minimum of the three // above calculated values return Math.min(AB, Math.min(AC, BC)); } // Driver Code let A = "aaa" ; let B = "bab" ; let C = "bbb" ; document.write(replacementOperations(A, B, C)); </script> |
1
Time Complexity: O(N)
Auxiliary Space: O(1)
Another Approach:
- Initialize the operation count for each pair of strings to zero.
- Traverse the strings A, B, and C simultaneously.
- If A[i] == B[i], increment the operation count for the pair AB by zero. If A[i] == C[i], increment the operation count for the pair AC by zero. If B[i] == C[i], increment the operation count for the pair BC by zero.
- If A[i] != B[i], increment the operation count for the pair AB by one. If A[i] != C[i], increment the operation count for the pair AC by one. If B[i] != C[i], increment the operation count for the pair BC by one.
- Find the minimum of the three operation counts and return it as the answer.
Below is the implementation of the above approach:
C++
// C++ program of the above approach #include <bits/stdc++.h> using namespace std; // 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) { // Initialize operation counts int AB = 0, AC = 0, BC = 0; // Traverse the strings simultaneously for ( int i = 0; i < A.length(); i++) { if (A[i] == B[i]) AB += 0; else AB += 1; if (A[i] == C[i]) AC += 0; else AC += 1; if (B[i] == C[i]) BC += 0; else BC += 1; } // Return the minimum of the three // operation counts 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 to find the minimum number of replacement operations // to make any two strings out of the given three strings equal import java.util.*; public class Main { static int replacementOperations(String A, String B, String C) { // Initialize operation counts int AB = 0 , AC = 0 , BC = 0 ; // Traverse the strings simultaneously for ( int i = 0 ; i < A.length(); i++) { if (A.charAt(i) == B.charAt(i)) { AB += 0 ; } else { AB += 1 ; } if (A.charAt(i) == C.charAt(i)) { AC += 0 ; } else { AC += 1 ; } if (B.charAt(i) == C.charAt(i)) { BC += 0 ; } else { BC += 1 ; } } // Return the minimum of the three operation counts 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)); } } |
Python3
# 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): # Initialize operation counts AB = 0 AC = 0 BC = 0 # Traverse the strings simultaneously for i in range ( len (A)): if A[i] = = B[i]: AB + = 0 else : AB + = 1 if A[i] = = C[i]: AC + = 0 else : AC + = 1 if B[i] = = C[i]: BC + = 0 else : BC + = 1 # Return the minimum of the three operation counts return min (AB, min (AC, BC)) # Driver code A = "aaa" B = "bab" C = "bbb" print (replacementOperations(A, B, C)) |
Javascript
function replacementOperations(A, B, C) { // Initialize operation counts let AB = 0, AC = 0, BC = 0; // Traverse the strings simultaneously for (let i = 0; i < A.length; i++) { if (A[i] === B[i]) AB += 0; else AB += 1; if (A[i] === C[i]) AC += 0; else AC += 1; if (B[i] === C[i]) BC += 0; else BC += 1; } // Return the minimum of the three operation counts return Math.min(AB, Math.min(AC, BC)); } // Driver code let A = "aaa" ; let B = "bab" ; let C = "bbb" ; console.log(replacementOperations(A, B, C)); |
1
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...