最小化替换以使三个给定字符串中的任意两个相等
给定三个长度相等的字符串A 、 B和C ,任务是找到可以执行的最小替换操作数,以使三个字符串中的任何两个字符串相等。
例子:
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.
方法:给定问题可以通过找出所有三种可能情况的操作计数来解决,即A = B或B = C或A = C ,并取三者中的最小值。使字符串X和Y相等所需的操作数可以通过遍历字符串并跟踪索引使得X[i] ≠ Y[i]来计算。此类索引的计数是所需的操作数。
下面是上述方法的实现:
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
输出
1
时间复杂度: O(N)
辅助空间: O(1)