📌  相关文章
📜  两个给定字符串之间的最小和最大差值之和

📅  最后修改于: 2021-09-06 06:24:12             🧑  作者: Mango

给定两个长度相同的字符串S1S2 。任务是找到最小和两个给定字符串之间的最大差异的总和,如果你被允许与其他任何字符替换字符串中的字符“+”。
注意:如果两个字符串在每个索引处都有相同的字符,则它们之间的差异为零。
例子:

方法:

  1. 最小差异:为了获得字符串之间的最小差异,可以将所有出现的“+”替换为相同的字符。因此,如果任何位置都有“+”,则可以使该特定索引等于另一个字符串。因此,我们只统计指标的数量,其中的字符不是“+”无论是在字符串,哪些不一样的字符。
  2. 最大差异:为了得到字符串之间的最大差异,所有出现的“+”都可以替换为不同的字符。因此,如果有一个“+”在任何位置,则该特定索引可以以比存在于所述第二字符串中的其它不同的字符来代替。因此,我们计算的指标的数量,其中的字符是“+”无论是在字符串,哪些不一样的字符。

下面是上述方法的实现:

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