- Strings

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:  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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.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. Therfore, 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: CPP#include using namespace std;  int minimumReplacement(string X, string Y){        int cnt = 0;    for (int i = 0; i < X.length(); i++)        if (X[i] != Y[i])            cnt++;          return cnt;}  int replacementOperations(    string A, string B, string C){        int AB = minimumReplacement(A, B);          int AC = minimumReplacement(A, C);          int BC = minimumReplacement(B, C);              return min(AB, min(AC, BC));}  int main(){    string A = "aaa";    string B = "bab";    string C = "bbb";      cout