Sum of minimum and the maximum difference between two given Strings - GeeksforGeeks

24 Dec, 2021
Given two strings S1 and S2 of the same length. The task is to find the sum of the minimum and the maximum difference between two given strings if you are allowed to replace the character ‘+’ in the strings with any other character.Note: If two strings have the same characters at every index, then the difference between them is zero.Examples: 
Input: S1 = “a+c”, S2 = “++b” Output: 4 Explanation: Minimum 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 = 4Input: S1 = “+++a”, S2 = “+++a” Output: 3 Explanation: Minimum 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 
Approach:  
Minimum Difference: In order to get the minimum difference between the strings, all the occurrences of “+” can be replaced by the same character. Therefore, if there is a “+” at any position, then that specific index can be made equal to the other string. Therefore, we only count the number of indices where the character is not “+” in either of the strings and the characters which are not same.Maximum Difference: In order to get the maximum difference between the strings, all the occurrences of “+” can be replaced by the different character. Therefore, if there is a “+” at any position, then that specific index can be replaced with a different character other than that present in the second string. Therefore, we count the number of indices where the character is “+” in either of the strings and the characters which are not same.
Minimum Difference: In order to get the minimum difference between the strings, all the occurrences of “+” can be replaced by the same character. Therefore, if there is a “+” at any position, then that specific index can be made equal to the other string. Therefore, we only count the number of indices where the character is not “+” in either of the strings and the characters which are not same.
Maximum Difference: In order to get the maximum difference between the strings, all the occurrences of “+” can be replaced by the different character. Therefore, if there is a “+” at any position, then that specific index can be replaced with a different character other than that present in the second string. Therefore, we count the number of indices where the character is “+” in either of the strings and the characters which are not same.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program to find the sum of the// minimum and the maximum difference// between two given strings#include <bits/stdc++.h>using namespace std; // Function to find the sum of the// minimum and the maximum difference// between two given stringsvoid 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 codeint main(){    string s1 = "a+c", s2 = "++b";    solve(s1, s2);    return 0;}
// Java program to find the sum of the// minimum and the maximum difference// between two given Stringsclass GFG{  // Function to find the sum of the// minimum and the maximum difference// between two given Stringsstatic 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 codepublic static void main(String[] args){    String s1 = "a+c", s2 = "++b";    solve(s1.toCharArray(), s2.toCharArray());}} // This code is contributed by Rajput-Ji
# 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 stringsdef 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 codeif __name__ == '__main__':    s1 = "a+c"    s2 = "++b"    solve(s1, s2) # This code is contributed by mohit kumar 29   
// C# program to find the sum of the// minimum and the maximum difference// between two given Stringsusing System; class GFG{   // Function to find the sum of the// minimum and the maximum difference// between two given Stringsstatic 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 codepublic static void Main(String[] args){    String s1 = "a+c", s2 = "++b";    solve(s1.ToCharArray(), s2.ToCharArray());}} // This code is contributed by Rajput-Ji
<script> // Javascript 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 stringsfunction solve(a, b){    var l = a.length;     // Variables to store the minimum    // difference and the maximum difference    var min = 0, max = 0;     // Iterate through the length of the string as    // both the given strings are of the same length    for (var 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++;    }     document.write(min + max);} // Driver codevar s1 = "a+c", s2 = "++b";solve(s1, s2);  </script>
4
 
Time Complexity: O(N)
mohit kumar 29
Rajput-Ji
noob2000
varshagumber28
germanshephered48
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Constructors in Java
Exceptions in Java
Data Types
Inline Functions in C++
Pure Virtual Functions and Abstract Classes in C++
Write a program to reverse an array or string
Write a program to print all permutations of a given string
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack
KMP Algorithm for Pattern Searching