Chef and Two Strings | CodeChef Solution

Hello coders, today we are going to solve Chef and Two Strings CodeChef Solution whose Problem Code is CHEFSTLT.

Chef and Two Strings

Task

Chef has found two very old sheets of paper, each of which originally contained a string of lowercase Latin letters. The strings on both the sheets have equal lengths. However, since the sheets are very old, some letters have become unreadable.

Chef would like to estimate the difference between these strings. Let’s assume that the first string is named S1, and the second S2. The unreadable symbols are specified with the question mark symbol ‘?’. The difference between the strings equals to the number of positions i, such that S1i is not equal to S2i, where S1i and S2i denote the symbol at the i the position in S1 and S2, respectively.

Chef would like to know the minimal and the maximal difference between the two strings, if he changes all unreadable symbols to lowercase Latin letters. Now that you’re fully aware of Chef’s programming expertise, you might have guessed that he needs you help solving this problem as well. Go on, help him!

Input Format

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of a test case contains a string S1.
The second line of a test case contains a string S2.
Both strings consist of lowercase Latin letters and question marks in places where the symbols are unreadable.

Output Format

For each test case, output the minimal and the maximal difference between two given strings separated with a single space.

Constraints

  • 1 ≤ T ≤ 100
  • 1 ≤ |S1|, |S2| ≤ 100
  • Subtask 1 (25 points): |S1| = 1
  • Subtask 2 (10 points): neither S1 nor S2 contains unreadable symbols
  • Subtask 3 (65 points): 1 ≤ |S1|, |S2| ≤ 100

Example

Sample Input

3
a?c
??b
???a
???a
?abac
aba?w

Sample Output

1 3
0 3
3 5

Solution – Chef and Two Strings | CodeChef Solution

C++

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t,diff,ques,i;
	string s1,s2;
	cin>>t;
	while(t--)
	{
	    cin>>s1;
	    cin>>s2;
	    ques=0;
	    diff=0;
	    for(i=0;i<s1.size();i++)
	    {
	        if(s1[i]=='?' || s2[i]=='?')
	        {
	            ques++;
	        }
	        else if (s1[i]!=s2[i])
	        {
	            diff++;
	        }
	    }
	    cout<<diff<<" "<<diff+ques<<endl;
	}
	
	return 0;
}

Python

# cook your dish here
for _ in range(int(input())):
    s1 = input()
    s2 = input()
    
    min_cnt = 0
    max_cnt = 0
    
    for i in range(len(s1)):
        if s1[i] == '?' or s2[i] == '?':
            max_cnt += 1
        elif s1[i] != s2[i]:
            min_cnt += 1
            max_cnt += 1
    print(min_cnt, max_cnt)

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		  Scanner sc = new Scanner(System.in);
        int tc = sc.nextInt();
        while (tc-- > 0) {
            String s1=sc.next();
            String s2=sc.next();
            int c=0,t=0;
            for(int i=0;i<s1.length();i++)
            {
                if( (s1.charAt(i)=='?' && s2.charAt(i)=='?') || (s1.charAt(i)=='?' && s2.charAt(i)!='?')|| (s2.charAt(i)=='?' && s1.charAt(i)!='?'))
                    c++;
              else  if(s1.charAt(i)!=s2.charAt(i) && s1.charAt(i)!='?' && s2.charAt(i)!='?')
                   t++;
            }
            System.out.println(t+" "+(t+c));

            }
	}
}

Disclaimer: The above Problem (Chef and Two Strings) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *