Find minimum difference and maximum difference in given String
Last Updated :
16 Sep, 2023
Given an input string that contains lowercase alphabets and the ‘?’ symbol, the task is to find the minimum and maximum possible difference by replacing ‘?’ and comparing s[i] with s[i+(n/2)].
Examples:
Input: s = “a?b??c”
Output: 1 3
Explanation: We split the input string in two equal parts s1 and s2. s1 = “a?b” and s2 = “??c”
- For minimum difference: we take s1 as “aab” and s2 as “aac”. s1[0]==s2[0], s1[1]==s2[1] and s1[2]!=s2[2] .So, the minimum difference here is 1.
- For maximum difference: we take s1 as “aab” and s2 as “bbc”. s1[0]!=s2[0], s1[1]!=s2[1] and s1[2]!=s2[2] .So, the maximum difference will be 3.
Input: s = “???c???c”
Output: 0 3
Explanation: We split the input string in two equal parts s1 and s2, s1 = “???c” and s2 = “???c”.
- For minimum difference: we take s1 as “aaac” and s2 as “aaac”. s1[0]==s2[0], s1[1]==s2[1], s1[2]==s2[2], s1[3]==s2[3] .So, minimum difference here will be 0.
- For maximum difference: we take s1 as “aaac” and s2 as “bbbc”. s1[0]!=s2[0], s1[1]!=s2[1], s1[2]!=s2[2], s1[3]==s2[3] .So here the maximum difference will be 3.
Approach: This can be solved with the following idea:
The idea is to break the input string into two parts and track the fixed alphabet’s difference and count of ‘?’ in both the strings .
The below codes is the implementation of the above approach:
C++
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
void minMax(string s)
{
string s1 = s.substr(0, (s.size()) / 2),
s2 = s.substr(s.size() / 2, s.size() / 2);
int count1 = 0, count2 = 0, diff = 0;
for ( int i = 0; i < s1.size(); i++) {
if (s1[i] == '?' )
count1++;
if (s2[i] == '?' )
count2++;
if (s1[i] != '?' && s2[i] != '?' )
if (s1[i] != s2[i])
diff++;
}
cout << diff << " " << max(count1, count2) + diff;
}
int main()
{
string s = "a?b??c" ;
minMax(s);
return 0;
}
|
Java
public class GFG {
public static void minMax(String s)
{
String s1 = s.substring( 0 , s.length() / 2 );
String s2 = s.substring(s.length() / 2 );
int count1 = 0 , count2 = 0 , diff = 0 ;
for ( int i = 0 ; i < s1.length(); i++) {
if (s1.charAt(i) == '?' )
count1++;
if (s2.charAt(i) == '?' )
count2++;
if (s1.charAt(i) != '?' && s2.charAt(i) != '?' )
if (s1.charAt(i) != s2.charAt(i))
diff++;
}
System.out.println(
diff + " " + (Math.max(count1, count2) + diff));
}
public static void main(String[] args)
{
String s = "a?b??c" ;
minMax(s);
}
}
|
Python3
def minMax(s):
s1 = s[: len (s) / / 2 ]
s2 = s[ len (s) / / 2 :]
count1 = 0
count2 = 0
diff = 0
for i in range ( len (s1)):
if s1[i] = = '?' :
count1 + = 1
if s2[i] = = '?' :
count2 + = 1
if s1[i] ! = '?' and s2[i] ! = '?' :
if s1[i] ! = s2[i]:
diff + = 1
print (diff, max (count1, count2) + diff)
s = "a?b??c"
minMax(s)
|
C#
using System;
public class GFG {
static void MinMax( string s)
{
string s1 = s.Substring(0, s.Length / 2);
string s2 = s.Substring(s.Length / 2, s.Length / 2);
int count1 = 0, count2 = 0, diff = 0;
for ( int i = 0; i < s1.Length; i++) {
if (s1[i] == '?' )
count1++;
if (s2[i] == '?' )
count2++;
if (s1[i] != '?' && s2[i] != '?' ) {
if (s1[i] != s2[i])
diff++;
}
}
Console.WriteLine($ "{diff} {Math.Max(count1, count2) + diff}" );
}
public static void Main( string [] args)
{
string s = "a?b??c" ;
MinMax(s);
}
}
|
Javascript
<script>
function minMax(s) {
const halfSize = Math.floor(s.length / 2);
const s1 = s.substring(0, halfSize);
const s2 = s.substring(halfSize, s.length);
let count1 = 0, count2 = 0, diff = 0;
for (let i = 0; i < s1.length; i++) {
if (s1[i] === '?' ) count1++;
if (s2[i] === '?' ) count2++;
if (s1[i] !== '?' && s2[i] !== '?' && s1[i] !== s2[i]) diff++;
}
document.write(diff + " " + (Math.max(count1, count2) + diff));
}
const s = "a?b??c" ;
minMax(s);
</script>
|
Time Complexity: O(N) //where N is the size of string s1 or s2.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...