[Solved] Caption Contest with Java, C++, Python

Exeter Caption Contest is a competition open to all writers worldwide. The entrants will have one day to compose and submit a caption that will be based on the theme posted on the competition page.

Robin, a creative writer had penned two captions for the contest but he unknowingly misplaced them. After searching long, he managed to locate his captions, but some letters in them have become unreadable. The captions were in two very old sheets of paper, each of which originally contained a string of lowercase English letters. The strings on both the sheets have equal lengths.

Robin 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 th position in S1 and S2, respectively.

Robin would like to know the minimal and the maximal difference between the two strings, if he changes all unreadable symbols to lowercase English letters. Robin is not an expertise in programming and so he needs your help solving this problem.

Input Format:
The first line of the input contains a string S1.
The second line of the input contains a string S2.
Both strings consist of lowercase English letters and question marks in places where the symbols are unreadable.

Output Format:
Output the minimal and the maximal difference between two given strings separated with a single space.
Refer sample input and output for formatting specifications.

Sample Input1:
a?c
??b
Sample Output1:
1 3

Sample Input2:
???a
???a
Sample Output2:
0 3

Solution

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        String name1,name2;
        int count=0,countq=0,i;
        Scanner sc=new Scanner(System.in);
        name1=sc.next();
        name2=sc.next();
        if(name1.length()==name2.length())
        {
            for(i=0;i<name1.length();i++)
            {
                //sauravhathi
                if((name1.charAt(i)!='?'&&(name2.charAt(i)!='?')&&(name1.charAt(i)!=name2.charAt(i))))
                {
                    count++;
                }
            }
        
        for(i=0;i<name1.length();i++)
        {
            //sauravhathi
            if((name1.charAt(i)=='?') || (name2.charAt(i)=='?'))
            {
                countq++;
            }
        }
        }
        countq=count+countq;
        //sauravhathi
        System.out.println(count+" "+countq);
    }
}
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char name1[100], name2[100];
    int count = 0, countq = 0, i;
    cin >> name1;
    // sauravhathi
    cin >> name2;
    if (strlen(name1) == strlen(name2))
    {
        for (i = 0; i < strlen(name1); i++)
        {
            if ((name1[i] != '?' && (name2[i] != '?') && (name1[i] != name2[i])))
            {
                //sauravhathi
                count++;
            }
        }

        for (i = 0; i < strlen(name1); i++)
        {
            if ((name1[i] == '?') || (name2[i] == '?'))
            {
                // sauravhathi
                countq++;
            }
        }
    }
    countq = count + countq;
    cout << count << " " << countq;
    return 0;
}
name1 = input();
name2 = input();
count=0
countq=0

if len(name1) == len(name2):
    for i in range(len(name1)):
        if (name1[i] != '?' and name2[i] != '?' and name1[i] != name2[i]):
            count += 1
#sauravhathi
        if (name1[i] == '?' or name2[i] == '?'):
            countq += 1
countq = count + countq
print(count, countq)

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

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