TWOSTR - Editorial

PROBLEM LINK:

Practice
Contest

Author: Tasnim Imran Sunny
Tester: Istvan Nagy
Editorialist: Lalit Kundu

DIFFICULTY:

Cakewalk

PREREQUISITES:

basic programming, strings

PROBLEM:

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.

EXPLANATION:

================
We can reduce problem of matching two strings X and Y to matching individual characters for each index 0 \le i < N. If all characters can be matched, then we can say that both strings can also be matched.

MATCHING A CHARACTER

We need to check if two characters a and b can be matched or not. If either of them is ‘?’, then we can always match them by filling it with the required value. If both are ‘?’, still we can give any same value to both of them.

If both are not ‘?’, then we just need to check if the current values are same or not.

For implementation, see setter’s commented code.

AUTHOR’S, TESTER’S SOLUTIONS:

setter
tester

1 Like

such a bonus question!

1 Like

can any one plzz tell what is wrong in my code
it is showing wrong answer

#include<stdio.h>
int main()
{
int t,i,flag;
char x[10],y[10];
scanf("%d",&t);
while(t–)
{
flag=0;
scanf("%s",x);
scanf("%s",y);
for(i=0;(x[i]!=NULL)&&(flag==0);i++)
{
if(x[i]!=’?’&&y[i]!=’?’)
{
if(x[i]!=y[i])
{
flag=1;
}
}
}
if(flag==0)
printf(“Yes\n”);
else
printf(“No\n”);
}
return 0;
}

#include
#include<string.h>
using namespace std;
int main()
{
int t,flag,i;
char str1[10],str2[10];
cin>>t;
while(t–)
{
cin>>str1;
cin>>str2;
int length=strlen(str1);
for(i=0;i<length;i++)
{

	**strong text**	if(str1[i]=='?'||str2[i]=='?')
			flag=1;
		else if(str1[i]==str2[i])
			flag=1;
		else
		{
			flag=0;
			break;
		}
	
	}
	if(flag==1)
		cout<<"Yes"<<endl;
	else
		cout<<"No"<<endl;
}
return 0;

}

#include<stdio.h>
int length(char str[]);
int main()
{
char string1[10],string2[10];
int i,sum=0,t,count;
scanf("%d",&t);
while(t–)
{
scanf("%s%s",&string1,&string2);
count=length(string1);
for(i=0;i<count;i++)
{
if(string1[i]==string2[i])
{
sum++;
}
else if(string1[i]==’?’)
{
sum++;
}
else if(string2[i]==’?’)
{
sum++;
}
}
if(sum==count)
{
printf(“Yes\n”);
}
else{
printf(“No\n”);
}
sum=0;
}
return 0;
}
int length(char str[])
{
int count=0;
int i=0;
while(str[i]!=’\0’)
{
count++;
i++;
}
return count;
}
what is wrong is this code why wrong anser are come

can anyone please tell me what is wrong with this code?

 for _ in range(int(input())):
    x=list(input())
    y=list(input())
    a=1
    for i,j in zip(x,y):
            if i==j or i=="?" or j=="?":
                    continue
            else:
                    print("NO")
                    a=0
                    break
    if a!=0:
            print("Yes")

you have to declare your char ARRAYS as x[11] and y[11] as strings have one character extra regarded as NULL

2 Likes

yes it worked with 11…

the print(i,j) is just for debugging purposes

CodeChef: Practical coding for everyone what is wrong in this code…

#include<bits/stdc++.h>

#define ll long long

#define testCases int t; cin >> t; while(t–)

using namespace std;

void solution();

int main(){

ios_base::sync_with_stdio(false);

cin.tie(NULL);

solution();



return 0;

}

void solution(){

testCases{

    string x, y;

    bool flag = 1;

    cin >> x >> y;

    for (int i = 0; i < x.length(); i++){

        if(x[i] != '?' && y[i] != '?' && x[i] != y[i]){

            flag = 0;

            break;

        }

    }

    cout << (flag?"Yes":"No") << '\n';

}

}

I think you @singhutsav will not be happy if you know the error.
You typed capital O instead of small o in print statement. Haha.
I spent 10 mins debugging and later found it, since the same logic in c was accepted

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

class Codechef
{
public static void main (String[] args)
{
Scanner sc =new Scanner(System.in);
int t,flag=0;
String x,y;
t=sc.nextInt();
x=sc.next();
y=sc.next();
for(int m=0;m<=x.length();m=m+1){

    if((x.charAt(m)==y.charAt(m))||((x.charAt(m)=='?')||(y.charAt(m)=='?')))
    {
        
        flag=1;
        
        
    }
    else{
        
        flag=0;
    }
}
if(flag==1){
    
    System.out.println("Yes");
    
}else{
    
    System.out.println("No");
    
}


}

}

its showing runtime error, can u plz help me with this code?

Can anyone identify the mistake in my approach

Python 3.6

[ CodeChef: Practical coding for everyone ]

def solution():
    X = input()
    Y = input()

    if len(X) != len(Y):
        return "NO"

    for x, y in zip(X, Y):
        if x != y and x != '?' and y != '?':
            return "NO"

    return "YES"


for _ in range(int(input())):
    print(solution())

You are supposed to return “Yes” instead of “YES” and “No” instead of “NO”