Chef and Two Strings Codechef Solution: 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
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
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
Input: 3 a?c ??b ???a ???a ?abac aba?w Output: 1 3 0 3 3 5
Explanation
Example case 1. You can change the question marks in the strings so that you obtain S1 = abc and S2 = abb. Then S1 and S2 will differ in one position. On the other hand, you can change the letters so that S1 = abc and S2 = bab. Then, the strings will differ in all three positions.
Example case 2. Change the question marks this way: S1 = dcba, S2 = dcba, then the strings will differ in 0 positions. You can also change the question marks so that S1 = aaaa, S2 = dcba, then the strings will differ in 3 positions.
Example case 3. Change the question marks this way: S1 = aabac, S2 = abaaw, then the strings will differ in 3 positions. Then, change the question marks this way: S1 = xabac, S2 = abayw, then they will differ in 5 positions.
Chef and Two Strings CodeChef Solution in JAVA
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 { // your code goes here try{ Scanner s=new Scanner(System.in); BufferedWriter wr=new BufferedWriter(new OutputStreamWriter(System.out)); int t=s.nextInt(); while(t-- > 0) { //int n=s.nextInt(); String s1=s.next(); String s2=s.next(); int n=s1.length(); int max=0,min=0; for(int i=0;i<n;i++) { if(s1.charAt(i)=='?' || s2.charAt(i)=='?') max++; else if(s1.charAt(i)!=s2.charAt(i)) { max++; min++; } } wr.write(min+" "+max+"\n"); } wr.flush(); } catch(Exception e){} } }
Chef and Two Strings CodeChef Solution in CPP
#include <iostream> #include<bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { string s1,s2; cin>>s1>>s2; int l=s1.length(); int dmax=0,dmin=0; for(int i=0;i<l;i++) { if(s1[i]=='?' || s2[i]=='?') { dmax++; } else if(s1[i]!=s2[i]) { dmin++; } } cout<<dmin<<" "<<dmax+dmin<<endl; } return 0; }
Chef and Two Strings CodeChef Solution in Python
def main(): for _ in range(int(input())): s1 = input() s2 = input() could_be_same = 0 could_be_differ = 0 for ch1, ch2 in zip(s1, s2): if ch1 == '?' or ch2 == '?': could_be_differ += 1 could_be_same += 1 elif ch1 == ch2: could_be_same += 1 elif ch1 != ch2: could_be_differ += 1 print(len(s1) - could_be_same, could_be_differ) if __name__ == "__main__": main()
Disclaimer: The above Problem (Chef and Two Strings) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.