[Solved] Wildcard Matching with Java, C++, Python

Wildcard Matching: Sunil is a little scientist. Sunil has planned to design a wildcard pattern matcher to exhibit at the “Young Inventors”, a tech talent show organized at his school.
Sunil wanted to design the wildcard pattern matcher supporting the wildcard character ‘?’. The wildcard character ‘?’ can be substituted by any single lower case English letter for matching. He has two strings X and Y of equal length, made up of lower case letters and the character ‘?’.
Sunil wants your help in designing the device, to know whether the strings X and Y can be matched or not. Write a program to check whether the given strings can be matched or not.

Input Format:
First line of the input contains the string ‘X’.
Second line of the input contains the string ‘Y’.

Output Format:
Output a single line with the word “Yes”(without quotes) if the strings can be matched, otherwise output “No”(without quotes).
Refer sample input and output for formatting specifications.

Sample Input1:
s?or?
sco??
Sample Output1:
Yes

Sample Input2:
stor?
sco??
Sample Output2:
No

Solution

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

int main()
{
    char a[200], b[200];

    cin >> a;
    // sauravhathi
    cin >> b;
    int i, j, count = 0;
    for (i = 0, j = 0; i < strlen(a) && j < strlen(b); i++, j++)
    {
        if (a[i] == '?' || b[j] == '?')
            count++;
        else if (a[i] != b[j])
            break;
    }
    if (i == strlen(a) && j == strlen(b))
        // sauravhathi
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}
a = input()
b = input()
i = 0
j = 0
count = 0
while i < len(a) and j < len(b):
    if a[i] == '?' or b[j] == '?':
        count = count + 1
    else:
        if a[i] != b[j]:
        # sauravhathi
            break
    i = i + 1
    # sauravhathi
    j = j + 1
if i == len(a) and j == len(b):
    print("Yes")
else:
    print("No")

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 *