Divine Words Solution in JAVA

Problem

Chef has a certain liking towards certain English characters. He considers these characters beautiful. You are given a string S consisting of all the characters Chef finds beautiful.

Chef considers a word divine if each and every character present in the word is beautiful. Given a list of N words, determine which of them are divine.

Input Format

  • The first line of the input contains a string S, consisting of beautiful characters. Each character in S will appear only once.
  • The second line of the input contains an integer N.
  • Each of the following N lines contains a single string P_i, denoting the i^{th} word in the list.

Output Format

For each of the N words, output "Yes" (without quotes) if the word is divine and "No" (without quotes) otherwise.

Constraints

  • 1 \leq |S| \leq 26
  • 1 \leq N \leq 1000
  • 1 \leq |P_i| \leq 12
  • Each character in S will appear only once.
  • SP_i consist only of lowercase English alphabets.

Subtasks

  • Subtask 1 (30 points): |S| = 1
  • Subtask 2 (70 points) : Original constraints

Sample 1:

Input
Output
rested
3
terp
dest
nop
No
Yes
No

Explanation:

Test Case 1: Character p isn't beautiful. Therefore this word is not divine.

Test Case 2: Every character in the word d, e, s, t is beautiful. Therefore, this word is divine.





Program :


 /* 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

{

public static void main (String[] args) throws java.lang.Exception

{

// your code goes here

Scanner sr = new Scanner(System.in);

        String bchars = sr.nextLine();

        int n = sr.nextInt();

        String[] a = new String[n];

        sr.nextLine();

        for (int i = 0; i < n; i++) 

        {

            a[i] = sr.nextLine();

        }


        for (String s: a) 

        {

            boolean d = true;

            for (char c: s.toCharArray()) 

            {

                if (!bchars.contains("" + c)) 

                {

                    d = false;

                    break;

                }

            }


            if (d) 

                System.out.println("Yes");

            else 

                System.out.println("No");

        }

}

}

Post a Comment

0 Comments