You are not logged in. Please login at www.codechef.com to post your questions!

×

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

This question is marked "community wiki".

asked 23 Aug '15, 19:02

darkshadows's gravatar image

5★darkshadows ♦
3.0k93164187
accept rate: 12%

edited 09 Feb '16, 18:58

admin's gravatar image

0★admin ♦♦
19.8k350498541


such a bonus question!

link

answered 24 Aug '15, 01:23

atulshanbhag's gravatar image

4★atulshanbhag
22629
accept rate: 9%

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;
    }
link

answered 26 Sep '15, 18:45

deeksha_garg's gravatar image

3★deeksha_garg
254
accept rate: 0%

2

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

(26 Sep '15, 20:03) goyal_banna3★

yes it worked with 11....

(27 Sep '15, 17:13) deeksha_garg3★

include<iostream>

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;

}

link

answered 21 Mar '16, 01:55

nrathodiya's gravatar image

3★nrathodiya
6111
accept rate: 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

link

answered 03 May '17, 07:07

laukesh_123's gravatar image

0★laukesh_123
1
accept rate: 0%

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")
link

answered 23 Jan, 19:54

singhutsav's gravatar image

2★singhutsav
11
accept rate: 0%

edited 23 Jan, 20:26

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

(23 Jan, 19:55) singhutsav2★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,651
×1,647
×827
×349
×104
×81
×1

question asked: 23 Aug '15, 19:02

question was seen: 4,452 times

last updated: 23 Jan, 20:26