Studying Alphabet Codechef Solution|Problem Code:ALPHABET

Studying Alphabet Codechef Solution


Not everyone probably knows that Chef has younder brother Jeff. Currently Jeff learns to read.

He knows some subset of the letter of Latin alphabet. In order to help Jeff to study, Chef gave him a book with the text consisting of N words. Jeff can read a word iff it consists only of the letters he knows.

Now Chef is curious about which words his brother will be able to read, and which are not. Please help him!


The first line of the input contains a lowercase Latin letter string S, consisting of the letters Jeff can read. Every letter will appear in S no more than once.

The second line of the input contains an integer N denoting the number of words in the book.

Each of the following N lines contains a single lowecase Latin letter string Wi, denoting the ith word in the book.


For each of the words, output “Yes” (without quotes) in case Jeff can read it, and “No” (without quotes) otherwise.


  • 1 ≤ |S| ≤ 26
  • 1 ≤ N ≤ 1000
  • 1 ≤ |Wi| ≤ 12
  • Each letter will appear in S no more than once.
  • S, Wi consist only of lowercase Latin letters.


  • Subtask #1 (31 point)|S| = 1, i.e. Jeff knows only one letter.
  • Subtask #2 (69 point) : no additional constraints

Sample Input 1


Sample Output 1



The first word can be read.

The second word contains the letters d, o and g that aren’t known by Jeff.

Studying Alphabet – CodeChef Solution in JAVA

import java.util.Scanner;
import java.util.Set;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(;
		String S =;
		Set<Integer> knownLetters = S.chars().boxed().collect(Collectors.toSet());
		int N = sc.nextInt();
		for (int tc = 0; tc < N; tc++) {
			String W =;
			System.out.println(solve(knownLetters, W) ? "Yes" : "No");
	static boolean solve(Set<Integer> knownLetters, String W) {
		return W.chars().allMatch(ch -> knownLetters.contains(ch));

Disclaimer: The above Problem (Studying Alphabet) is generated by CodeChef but the solution is provided by  Chase2learn.This tutorial is only for Educational and Learning purpose.

Sharing Is Caring

Leave a Comment