ALPHABET - Editorial

#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int n;
cin>>n;
string w;
for(int i=0;i<n;i++)
{
int flag=0;
cin>>w;
for(int i=0;i<s.length();i++)
{
for(int j=0;j<w.length();j++)
{
if(s[i]==w[j])
{
flag=0;
break;
}
else
{
flag=1;
}
}
}
if(flag==1)
cout<<“No”<<endl;
else
cout<<“Yes”<<endl;
}
}

what is wrong with solution can anyone help me?

I hope this will Help You alot :
Just Go Through this

And Subscribe my channel Hello World Please.

can anyone tell me what is wrong with this code

#include <iostream>

using namespace std;



int main() {
	string jStr;
	int a[26]={0};
	cin>>jStr;
	for(int i=0;i<jStr.length();i++)
	{
		int j=(int)jStr[i]-97;
		a[j]=1;
	}
	int no;
	cin>>no;
	string str;
	int notPresent{0};
	while(no--)
	{
		cin>>str;
		for(int j=0;j<str.length();j++)
		{
			int temp=(int)str[j]-97;
			if(a[temp]==1)
			{
			    notPresent=0;
			}
				
			else
			{
				notPresent++;
			}
		}
		if(notPresent>0)
			cout<<"No"<<endl;
		else
			cout<<"Yes"<<endl;
	}
	
	return 0;
}

i have tested it with provided example

help with this code
#include
#include
using namespace std;

int main()
{

    string s;
    cin >> s;
    sort( s.begin(), s.end() );
    int n;
    cin >> n;
    while( n-- )
    {
        string s1;
        cin >> s1;
        sort( s1.begin(), s1.end() );
        if ( s == s1 )
        cout << "Yes" << endl;
        else
        cout << "No" << endl;
    }
    
return 0;

}

PLEASE CHECK MY CODE ONCE:
My almost all custom inputs are working well and giving out correct solutions, but while SUBMITTING its providing WRONG ANSWER

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
  string  s , t1;
  cin >> s;
  int j , n = s.length(); 
  char char_array[n + 1]; 

   strcpy(char_array, s.c_str()); 
   int t , i;
   cin >> t;
   for( i = 0 ; i < t ; i++ )
   {
      cin>>t1;
      int k = t1.length();
      char char_array2[k + 1]; 
      strcpy( char_array2, t1.c_str() );
      int m , a = 0;
      for( j = 0 ; j < = k ; j++ )
      {
        for( m = 0 ; m < = n ; m++ )
        {
            if( char_array2[j] == char_array[m] )
           {
                a++;
            }
        }
    }
    if(a!=n+1)
        cout << "No" << endl;
    else
        cout << "Yes" << endl;
  }
return 0;
}

Waiting for a soon REPLY!

#include<bits/stdc++.h>
using namespace std;

int main(){
string s;
getline(cin, s);
int st[26]={0};
for(int i=0; i<s.length(); i++){
st[(int)s[i]- 97]=1;
}
int n;
scanf("%d", &n);
while(n–){
string w; int c=0;
getline(cin, w); //##############This line############//
for(int i=0; i<w.length(); i++){
if(st[(int)w[i]- 97]==1){
++c;
}
}
if(c==w.length()) printf(“Yes\n”);
else printf(“No\n”);
}

}

In the above code, there’s a line marked “This line”. If I am using getline, my code was not accepted, but when I used cin, my code was accepted. Any idea why this happened?

in the 5th line you have left Ampersand …on scanf line .same mistake in while loop 3rd line “&ch”

#include<bits/stdc++.h>

#define ll long long

#define lli long long int

#define MOD 1000000007

#define input(a) cin >> a

#define print(a) cout << a

#define printsp(a) cout << a << " "

#define println(a) cout << a << endl

#define debug(a) cout << #a << " = " << a << “\n”

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

#define loop(n) for(ll int i = 0; i < n; i++)

#define iloop(k,n) for(ll int i = k; i < n; i++)

#define jloop(k,n) for(ll int j = k; j < n; j++)

#define reverseLoop(k,n) for(ll int i = k; i >= n; i–)

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)

using namespace std;

void solution(){

string s, word;

input(s);

int n;

input(n);



loop(n){

    bool flag = 1;

    input(word);

    set <char> letters ( begin(word), end(word) );

    

    for(auto it : letters){

        if(count(s.begin(), s.end(), it) == 0){

            flag = 0;

        }

    }

    println((flag ? "Yes": "No"));

}

}

int main(){

fastIO;

solution();



return 0;

}

1 Like

you’re wrong, what if the string is “pepsi”
and the ones to check for are “peppsi”,“peepsi”
They’re correct but your method will fail here :slight_smile:

#include <bits/stdc++.h>

using namespace std;

int main()

{

int t;

string s, s1;

cin >> s;

sort(s.begin(), s.end());

cin >> t;

while (t--)

{

    cin >> s1;

    sort(s1.begin(), s1.end());

    int count = 0;

    for (int i = 0; i < s.size(); i++)

    {

        if (s[i] == s1[i])

        {

            count++;

        }

    }

    if (count == s1.size())

    {

        cout << "Yes" << endl;

    }

    else

    {

        cout << "No" << endl;

    }

}

return 0;

}

//what’s wrong in my code why sort() was not working?

@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);
}

}