ALPHABET - Editorial

@gvlokesh9287
what if the alphabets I know are “abcde” and I only read something like “abcccc”. It should be correct but yours shows no.

https://www.codechef.com/viewsolution/56204590

Output is correct
Checked with many inputs
Still showing wrong, why?

Check the output format, NO and No are treated differently.

Rectified
#include<iostream>
using namespace std;

int main(){
    string s;
    int N;
    cin>>s;
    cin>>N;
    while (N--)
    {
        bool check = 1;
        string w;
        cin>>w;
        int H[26]={0};
        for (int i = 0; i < s.length(); i++)
        {
            H[s[i]-97]++;
        }
        for (int i = 0; i < w.length(); i++)
        {
            if (H[w[i]-97]<1)
            {
                check = false;
                break;
            }     
        }
        if (check==1)
        {
            cout<<"Yes"<<endl;
        }
        else
        {
            cout<<"No"<<endl;
        }       
    }  
    return 0;
}
1 Like

PLEASE TELL THE MISTAKE . IT IS PARTIALLY CORRECT
a=input()
p=set(a)
z=list(p)
z.sort()
c="".join(sorted(z))
b=int(input())
for i in range(b):
d=input()
x=set(d)
y=list(x)
y.sort()
g="".join(sorted(y))
if(c==g):
print(“Yes”)
else:
print(“No”)

how did you take string input in set??

I checked whether the set of characters in each input string is a subset of the set of characters Jeff can already read.

S = set(input())
N = int(input())

for i in range (N):
    input_chars = set(input())
    if input_chars.issubset(S):
        print("Yes")
    else:
        print("No")

What if a = “xyz” and g = “xy”? It should print “Yes” because “xy” contains characters he can read. But in your program, it would print “No”. Your a and g doesn’t have to be equal for him to be able to read it. Instead, characters in g have to be a subset of characters in a.

can anyone help out me with the error?

// Online C compiler to run C program online
#include <stdio.h>
#include<string.h>

int main() {
    // Write C code here
    char str[27];
    scanf("%s",&str);
    long long int n=0;
    scanf("%lld",&n);
    while(n--)
        {
            char str2[13];
            long long int count=0;
            scanf("%s",&str2);
            if(strlen(str2)==1)
                {
                    
                    for(int i=0;i<27;++i)
                        {
                            if(i==26)
                                {
                                    printf("No\n");
                                }
                            if(str2[1]==str[i])
                                {
                                    printf("Yes\n");
                                    break;
                                }
                        }
                }
                else 
                    {
            for(long long int i=0;i<13;++i)
                {
                    for(long long int j=0;j<27;++j)
                        {
                            
                            if(str2[i]>=97&&str2[i]<=122)
                            {
                                
                            if(str2[i]==str[j])
                                {
                                    count++;
                                    break;
                                }
                            else if(str2[i]!=str[j])
                                {
                                    continue;
                                }
                            }
                            else
                                {
                                    continue;
                                }
                            
                            
                        }
                        
                }
              
                if(count==strlen(str2))
                    {
                        printf("Yes\n");
                    }
                else
                    {
                        printf("No\n");
                    }
                    }
        }
    
    return 0;
}

It works fine… , for example, let us consider the input:
dog
1
o
it gives correct results for ‘o’, which is ofcourse “Yes”
i have read this whole editorial and edited the code according to what you guys said. I could not think of a better code than this(i’m only 12 years old)
please help me out.
it is showing “wrong answer”…

Hey @iron102 :wave: ,
Your code is correct only problem is on the for loop.You always run a loop for 27 for str and 13 for str2 here only your code is giving wrong even If input string is of size 5 for str but your code will still run for 27 making the logic wrong. Just put strlen(str) in place of 27 in every for loop and strlen(str2) in place of 13. Also make sure to handle the case when strlen(str2)==1 with this modification.
Here is your code with modification.

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

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    char str[27];
    scanf("%s", &str);
    long long int n = 0;
    scanf("%lld", &n);
    while (n--)
    {
        char str2[13];
        long long int count = 0;
        scanf("%s", &str2);
        if (strlen(str2) == 1)
        {

            for (int i = 0; i < strlen(str) + 1; ++i)
            {
                if (i == strlen(str))
                {
                    printf("No\n");
                    break;
                }
                if (str2[0] == str[i])
                {
                    printf("Yes\n");
                    break;
                }
            }
        }
        else
        {
            for (long long int i = 0; i < strlen(str2); ++i)
            {
                for (long long int j = 0; j < strlen(str); ++j)
                {

                    if (str2[i] >= 97 && str2[i] <= 122)
                    {

                        if (str2[i] == str[j])
                        {
                            count++;
                            break;
                        }
                        else if (str2[i] != str[j])
                        {
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }


                }

            }

            if (count == strlen(str2))
            {
                printf("Yes\n");
            }
            else
            {
                printf("No\n");
            }
        }
    }

    return 0;
}

What is wrong with my code?
/* package codechef; // don’t place package name! */

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

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
static void display(int[] x)
{
for(int i=0; i<x.length; i++)
{
System.out.print(x[i]+" ");
}
System.out.println();
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();

	int N = sc.nextInt();
	sc.nextLine();
	
    int l = s.length();
    int[] s1 = new int[26];
    for(int i=0; i<26; i++)
    {
        s1[i]=0;
    }
    
    for(int i=0; i<l; i++)
    {
        
        s1[(int)s.charAt(i)-'a'] = 1;
    }
	
	String r = "Yes";
	for(int i=0; i<N; i++)
	{
	    String w = sc.nextLine();
	    l=w.length();
	    for(int j=0; j<l; j++)
	    {
	        if(s1[(int)w.charAt(j)-'a']!=1)
	        {
	            r="No";
	            break;
	        }
	    }
	    System.out.println(r);
	}
//	display(s1);
}

}