Sum of minimum and the maximum difference between two given Strings
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 = 4
Input: 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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve(string a, string b)
{
int l = a.length();
int min = 0, max = 0;
for ( int i = 0; i < l; i++) {
if (a[i] == '+' || b[i] == '+' || a[i] != b[i])
max++;
if (a[i] != '+' && b[i] != '+' && a[i] != b[i])
min++;
}
cout << min + max << endl;
}
int main()
{
string s1 = "a+c" , s2 = "++b" ;
solve(s1, s2);
return 0;
}
|
Java
class GFG{
static void solve( char []a, char []b)
{
int l = a.length;
int min = 0 , max = 0 ;
for ( int i = 0 ; i < l; i++) {
if (a[i] == '+' || b[i] == '+' || a[i] != b[i])
max++;
if (a[i] != '+' && b[i] != '+' && a[i] != b[i])
min++;
}
System.out.print(min + max + "\n" );
}
public static void main(String[] args)
{
String s1 = "a+c" , s2 = "++b" ;
solve(s1.toCharArray(), s2.toCharArray());
}
}
|
Python3
def solve(a, b):
l = len (a)
min = 0
max = 0
for i in range (l):
if (a[i] = = '+' or b[i] = = '+' or a[i] ! = b[i]):
max + = 1
if (a[i] ! = '+' and b[i] ! = '+' and a[i] ! = b[i]):
min + = 1
print ( min + max )
if __name__ = = '__main__' :
s1 = "a+c"
s2 = "++b"
solve(s1, s2)
|
C#
using System;
class GFG{
static void solve( char []a, char []b)
{
int l = a.Length;
int min = 0, max = 0;
for ( int i = 0; i < l; i++) {
if (a[i] == '+' || b[i] == '+' || a[i] != b[i])
max++;
if (a[i] != '+' && b[i] != '+' && a[i] != b[i])
min++;
}
Console.Write(min + max + "\n" );
}
public static void Main(String[] args)
{
String s1 = "a+c" , s2 = "++b" ;
solve(s1.ToCharArray(), s2.ToCharArray());
}
}
|
Javascript
<script>
function solve(a, b)
{
var l = a.length;
var min = 0, max = 0;
for ( var i = 0; i < l; i++) {
if (a[i] == '+' || b[i] == '+' || a[i] != b[i])
max++;
if (a[i] != '+' && b[i] != '+' && a[i] != b[i])
min++;
}
document.write(min + max);
}
var s1 = "a+c" , s2 = "++b" ;
solve(s1, s2);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1) as it is using constant space for variables
Last Updated :
19 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...