A special site for solving fun programming problems and challenges, interested in computer science, programming, basics, data structure and algorithms

 


Chef and the Wildcard Matching Problem Code: TWOSTRSolvedSubmit

Read problems statements in Mandarin Chinese and Russian as well.

Chef wants to implement wildcard pattern matching supporting only the wildcard '?'. 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 '?'. He wants to know whether the strings X and Y can be matched or not.

Input

The first line of input contain an integer T denoting the number of test cases. Each test case consists of two lines, the first line contains the string X and the second contains the string Y.

Output

For each test case, output a single line with the word Yes if the strings can be matched, otherwise output No.

Constraints

  • 1 ≤ T ≤ 50
  • Both X and Y have equal length and the length is between 1 and 10.
  • Both X and Y consist of lower case letters and the character '?'.

Sample Input 1 

2
s?or?
sco??
stor?
sco??

Sample Output 1 

Yes
No

Explanation

First Example: There are several ways the two strings can be matched, one of those is "score".

Second Example: There is no way to match the strings.


solution in cpp

#include<iostream>
#include<string>
using namespace std;

class solution
{
public:
	void solve()
	{
		int cnt = 0;
		string s1, s2;
		cin >> s1 >> s2;
		int n1 = s1.length();
		for (int i = 0; i < n1; i++)
		{
			if (s1[i] == s2[i])
				cnt++;
			else if ((s1[i] == '?' && s2[i] != '?') || (s1[i] != '?' && s2[i] == '?')|| (s1[i] == '?' && s2[i] == '?'))
				cnt++;
		}
		if (cnt == n1)
			cout << "Yes\n";
		else
			cout << "No\n";
	}
};
int main()
{
	solution ss;

	int t;
	cin >> t;
	while (t--)
    {
		ss.solve();
	}

	return 0;
}

No comments:

Post a Comment