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++
#include <bits/stdc++.h>
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 << replacementOperations(A, B, C);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int minimumReplacement(String X, String Y)
{
int cnt = 0 ;
for ( int i = 0 ; i < X.length(); i++)
if (X.charAt(i) != Y.charAt(i))
cnt++;
return cnt;
}
static int replacementOperations(
String A, String B, String C)
{
int AB = minimumReplacement(A, B);
int AC = minimumReplacement(A, C);
int BC = minimumReplacement(B, C);
return Math.min(AB, Math.min(AC, BC));
}
public static void main(String args[])
{
String A = "aaa" ;
String B = "bab" ;
String C = "bbb" ;
System.out.println(replacementOperations(A, B, C));
}
}
|
Python3
def minimumReplacement(X, Y):
cnt = 0
for i in range ( len (X)):
if (X[i] ! = Y[i]):
cnt + = 1
return cnt
def replacementOperations(A, B, C):
AB = minimumReplacement(A, B)
AC = minimumReplacement(A, C)
BC = minimumReplacement(B, C)
return min (AB, min (AC, BC))
A = "aaa"
B = "bab"
C = "bbb"
print (replacementOperations(A, B, C))
|
C#
using System;
using System.Collections;
class GFG
{
static 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;
}
static int replacementOperations(
string A, string B, string C)
{
int AB = minimumReplacement(A, B);
int AC = minimumReplacement(A, C);
int BC = minimumReplacement(B, C);
return Math.Min(AB, Math.Min(AC, BC));
}
public static void Main()
{
string A = "aaa" ;
string B = "bab" ;
string C = "bbb" ;
Console.Write(replacementOperations(A, B, C));
}
}
|
Javascript
<script>
function minimumReplacement(X, Y) {
let cnt = 0;
for (let i = 0; i < X.length; i++)
if (X[i] != Y[i])
cnt++;
return cnt;
}
function replacementOperations(A, B, C) {
let AB = minimumReplacement(A, B);
let AC = minimumReplacement(A, C);
let BC = minimumReplacement(B, C);
return Math.min(AB, Math.min(AC, BC));
}
let A = "aaa" ;
let B = "bab" ;
let C = "bbb" ;
document.write(replacementOperations(A, B, C));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int replacementOperations(
string A, string B, string C)
{
int AB = 0, AC = 0, BC = 0;
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 min(AB, min(AC, BC));
}
int main()
{
string A = "aaa" ;
string B = "bab" ;
string C = "bbb" ;
cout << replacementOperations(A, B, C);
return 0;
}
|
Java
import java.util.*;
public class Main {
static int replacementOperations(String A, String B, String C) {
int AB = 0 , AC = 0 , BC = 0 ;
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 Math.min(AB, Math.min(AC, BC));
}
public static void main(String[] args) {
String A = "aaa" ;
String B = "bab" ;
String C = "bbb" ;
System.out.println(replacementOperations(A, B, C));
}
}
|
Python3
def replacementOperations(A, B, C):
AB = 0
AC = 0
BC = 0
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 min (AB, min (AC, BC))
A = "aaa"
B = "bab"
C = "bbb"
print (replacementOperations(A, B, C))
|
C#
using System;
public class Program {
public static int
ReplacementOperations( string A, string B, string C)
{
int AB = 0, AC = 0, BC = 0;
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 Math.Min(AB, Math.Min(AC, BC));
}
public static void Main()
{
string A = "aaa" ;
string B = "bab" ;
string C = "bbb" ;
Console.WriteLine(ReplacementOperations(A, B, C));
}
}
|
Javascript
function replacementOperations(A, B, C) {
let AB = 0, AC = 0, BC = 0;
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 Math.min(AB, Math.min(AC, BC));
}
let A = "aaa" ;
let B = "bab" ;
let C = "bbb" ;
console.log(replacementOperations(A, B, C));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
03 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...