给定两个长度相同的字符串S1和S2 。任务是找到最小和两个给定字符串之间的最大差异的总和,如果你被允许与其他任何字符替换字符串中的字符“+”。
注意:如果两个字符串在每个索引处都有相同的字符,则它们之间的差异为零。
例子:
Input: S1 = “a+c”, S2 = “++b”
Output: 4
Explanation:
Minimim difference = 1. For minimum difference:
In the string S1 = “a+c”, the “+” can be replaced by “b” thereby making S1 = “abc”
In the string S2 = “++b”, the “++” can be replaced by “ab” thereby making S2 = “abb”
Above, only the 3rd index has a different character. So, the minimum difference = 1
Maximum difference = 1. For maximum difference:
In the string S1 = “a+c”, the “+” can be replaced by “z” thereby making S1 = “azc”
In the string S2 = “++b”, the “++” can be replaced by “bz” thereby making S2 = “bzb”
Above, all the indices has the difference characters. So, the maximum difference = 3
Minimum Difference + Maximum Difference = 1 + 3 = 4
Input: S1 = “+++a”, S2 = “+++a”
Output: 3
Explanation:
Minimim difference = 0. For minimum difference:
In the string S1 = “+++a”, the “+++” can be replaced by “aaa” thereby making S1 = “aaaa”
In the string S2 = “+++a”, the “+++” can be replaced by “aaa” thereby making S2 = “aaaa”
Above, all the indices has the same characters. So, the minimum difference = 0
Maximum difference = 3. For maximum difference:
In the string S1 = “+++a”, the “+++” can be replaced by “aaa” thereby making S1 = “aaaa”
In the string S2 = “+++a”, the “+++” can be replaced by “bbb” thereby making S2 = “bbba”
Above, all the indices except the last has the difference characters. So, the maximum difference = 3
Minimum Difference + Maximum Difference = 0 + 3 = 3
方法:
- 最小差异:为了获得字符串之间的最小差异,可以将所有出现的“+”替换为相同的字符。因此,如果任何位置都有“+”,则可以使该特定索引等于另一个字符串。因此,我们只统计指标的数量,其中的字符不是“+”无论是在字符串,哪些不一样的字符。
- 最大差异:为了得到字符串之间的最大差异,所有出现的“+”都可以替换为不同的字符。因此,如果有一个“+”在任何位置,则该特定索引可以以比存在于所述第二字符串中的其它不同的字符来代替。因此,我们计算的指标的数量,其中的字符是“+”无论是在字符串,哪些不一样的字符。
下面是上述方法的实现:
C++
// C++ program to find the sum of the
// minimum and the maximum difference
// between two given strings
#include
using namespace std;
// Function to find the sum of the
// minimum and the maximum difference
// between two given strings
void solve(string a, string b)
{
int l = a.length();
// Variables to store the minimum
// difference and the maximum difference
int min = 0, max = 0;
// Iterate through the length of the string as
// both the given strings are of the same length
for (int i = 0; i < l; i++) {
// For the maximum difference, we can replace
// "+" in both the strings with different char
if (a[i] == '+' || b[i] == '+' || a[i] != b[i])
max++;
// For the minimum difference, we can replace
// "+" in both the strings with the same char
if (a[i] != '+' && b[i] != '+' && a[i] != b[i])
min++;
}
cout << min + max << endl;
}
// Driver code
int main()
{
string s1 = "a+c", s2 = "++b";
solve(s1, s2);
return 0;
}
Java
// Java program to find the sum of the
// minimum and the maximum difference
// between two given Strings
class GFG{
// Function to find the sum of the
// minimum and the maximum difference
// between two given Strings
static void solve(char []a, char []b)
{
int l = a.length;
// Variables to store the minimum
// difference and the maximum difference
int min = 0, max = 0;
// Iterate through the length of the String as
// both the given Strings are of the same length
for (int i = 0; i < l; i++) {
// For the maximum difference, we can replace
// "+" in both the Strings with different char
if (a[i] == '+' || b[i] == '+' || a[i] != b[i])
max++;
// For the minimum difference, we can replace
// "+" in both the Strings with the same char
if (a[i] != '+' && b[i] != '+' && a[i] != b[i])
min++;
}
System.out.print(min + max +"\n");
}
// Driver code
public static void main(String[] args)
{
String s1 = "a+c", s2 = "++b";
solve(s1.toCharArray(), s2.toCharArray());
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to find the sum of the
# minimum and the maximum difference
# between two given strings
# Function to find the sum of the
# minimum and the maximum difference
# between two given strings
def solve(a, b):
l = len(a)
# Variables to store the minimum
# difference and the maximum difference
min = 0
max = 0
# Iterate through the length of the as
# both the given strings are of the same length
for i in range(l):
# For the maximum difference, we can replace
# "+" in both the strings with different char
if (a[i] == '+' or b[i] == '+' or a[i] != b[i]):
max += 1
# For the minimum difference, we can replace
# "+" in both the strings with the same char
if (a[i] != '+' and b[i] != '+' and a[i] != b[i]):
min += 1
print(min + max)
# Driver code
if __name__ == '__main__':
s1 = "a+c"
s2 = "++b"
solve(s1, s2)
# This code is contributed by mohit kumar 29
C#
// C# program to find the sum of the
// minimum and the maximum difference
// between two given Strings
using System;
class GFG{
// Function to find the sum of the
// minimum and the maximum difference
// between two given Strings
static void solve(char []a, char []b)
{
int l = a.Length;
// Variables to store the minimum
// difference and the maximum difference
int min = 0, max = 0;
// Iterate through the length of the String as
// both the given Strings are of the same length
for (int i = 0; i < l; i++) {
// For the maximum difference, we can replace
// "+" in both the Strings with different char
if (a[i] == '+' || b[i] == '+' || a[i] != b[i])
max++;
// For the minimum difference, we can replace
// "+" in both the Strings with the same char
if (a[i] != '+' && b[i] != '+' && a[i] != b[i])
min++;
}
Console.Write(min + max +"\n");
}
// Driver code
public static void Main(String[] args)
{
String s1 = "a+c", s2 = "++b";
solve(s1.ToCharArray(), s2.ToCharArray());
}
}
// This code is contributed by Rajput-Ji
Javascript
4
时间复杂度: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live