Open In App

Find minimum difference and maximum difference in given String

Last Updated : 16 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
 
// Function to find min Max difference
void minMax(string s)
{
 
    // Splitting the input string
    // in two equal parts
    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++;
    }
 
    // Output the result
    cout << diff << " " << max(count1, count2) + diff;
}
 
// Driver code
int main()
{
    string s = "a?b??c";
 
    // Function call
    minMax(s);
 
    return 0;
}


Java




// Java code for the above approach:
 
public class GFG {
    // Function to find min Max difference
    public static void minMax(String s)
    {
        // Splitting the input string in two equal parts
        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++;
        }
 
        // Output the result
        System.out.println(
            diff + " " + (Math.max(count1, count2) + diff));
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "a?b??c";
 
        // Function call
        minMax(s);
    }
}
 
//  This code is contributed by rambabuguphka


Python3




def minMax(s):
    # Splitting the input string in two equal parts
    s1 = s[:len(s)//2# First half of the string
    s2 = s[len(s)//2:]  # Second half of the string
     
    count1 = 0  # Counter for '?' in s1
    count2 = 0  # Counter for '?' in s2
    diff = 0    # Counter for differing characters between s1 and s2
     
    # Iterate over the characters in s1 and s2 simultaneously
    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
 
    # Output the result
    print(diff, max(count1, count2) + diff)
 
 
# Driver code
s = "a?b??c"
 
# Function call
minMax(s)
 
# This code is contributed by shivamgupta0987654321


C#




// C# code for the above approach
using System;
 
public class GFG {
    // Function to find min Max difference
    static void MinMax(string s)
    {
        // Splitting the input string
        // into two equal parts
        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++;
            }
        }
 
        // Output the result
        Console.WriteLine($"{diff} {Math.Max(count1, count2) + diff}");
    }
 
    public static void Main(string[] args)
    {
        string s = "a?b??c";
 
        // Function call
        MinMax(s);
    }
}
 
// This code is contributed by Susobhan Akhuli


Javascript




<script>
// Javascript code for the above approach
 
// Function to find min Max difference
function minMax(s) {
    // Splitting the input string
    // in two equal parts
    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++;
    }
 
    // Output the result
    document.write(diff + " " + (Math.max(count1, count2) + diff));
}
 
// Driver code
const s = "a?b??c";
 
// Function call
minMax(s);
 
// This code is contributed by Susobhan Akhuli
</script>


Output

1 3

Time Complexity: O(N) //where N is the size of string s1 or s2.
Auxiliary Space: O(1) 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads