Admission in Ninja Gram

Easy
0/40
Average time to solve is 15m
48 upvotes
Asked in companies
Zoho CorporationSnapdeal Ltd.Hexaview Technologies

Problem statement

Ninja is shifting to a new place named NinjaGram. To take admission to the new school, the teacher gives him a sentence and asks him to check whether the sentence is a pangram.

A word or a sentence is called a pangram if it contains all the English alphabet letters.

Since Ninja is new to programming, he doesn’t have much experience; he asks you to solve the problem. Can you help Ninja figure out whether the sentence is a pangram?

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer ‘T’ denoting the number of test cases. The test cases follow.

The first line of each test case contains a number ‘n’ denoting the number of characters in the string.

The second line of each test case contains the string where the string characters can be both uppercase and lowercase.
Output Format:
For each test case, if Ninja managed to solve the problem, print “YES” else “NO”.

Print the output of each test case in a separate line.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function.
Constraints:
1<= T <= 50
1<= n <= 10^4


Where ’T’ is the number of test cases, ‘n’ denotes the number of characters in the string.

Time Limit: 1 sec
Sample Input 1:
2
12
toosmallword
35
TheQuickBrownFoxJumpsOverTheLazyDog
Sample Output 1:
NO
YES
Explanation for Sample Input 1:
In the first test case, the given string is “toosmallword” which contains the characters [‘t’,’o’,’s’,’m’,’a’,’l’,’w’,’r’,’d’], which doesn’t contain all the 26 alphabetic characters. Hence the answer is “NO”.


In the second test case, the given string is “TheQuickBrownFoxJumpsOverTheLazyDog” which contains the characters: [‘t’,’h’,’e’,’q’,’u’,’i’,’c’,’k’,’b’,’r’,’o’,’w’,’n’,’f’,’x’,’j’,’m’,’p,’’s’,’v’,’l,’a’,’z’,’y’,’ d’,’g’], which contains all the 26 alphabetic characters. Hence the answer is “YES”.

Sample Input 2:

2
16
CodingNinjas
10
CodeStudio
Sample Output 2:
NO
NO
Explanation for Sample Input 2:
In the first test case, the given string is “CodingNinjas” which contains the characters [‘c’,’o’,’d’,’i’,’n’,’g’,’i’,’j’,’a’,’s’], which doesn’t contain all the 26 alphabetic characters. Hence the answer is “NO”.


In the first test case, the given string is “CodeStudio” which contains the characters [‘c’,’o’,’d’,’e’,’s’,’t’,’u’,’i’], which doesn’t contain all the 26 alphabetic characters. Hence the answer is “NO”.
Hint

 Will it be feasible to maintain a boolean array to check for each character in the string?

Approaches (1)
Brute Force

The idea is to maintain a boolean array that will check for all the characters in the string.

 

The steps are as follows:

  • We will maintain a boolean visited array of size 26 and initialized with false, which will contain whether a particular character has been seen or not.
  • We will traverse a loop starting from ‘i’=0 and to the size of the string - 1:
    • Whenever we see a character we mark it as true.
    • To take care of all case characters, lowercase and uppercase are considered the same. So ‘A’ and ‘a’ are marked in index 0 and similarly ‘Z’ and ‘z’ are marked in index 25.
  • After iterating through all the characters of the string, we will check whether all the characters from ‘a’ to ‘z’ are marked or not.
  • If any character is not marked then return false as this is not a pangram. Otherwise return true.
Time Complexity

O(N), where N is the number of characters in the string.

 

We are traversing a loop only once and checking if all the 26 characters are present in the string. Hence, the overall time complexity is O(N).

Space Complexity

O(1), no extra space required.

 

As we are not using any extra space except for an array to maintain 26 characters. Hence, the overall space complexity is O(1).


 

Code Solution
(100% EXP penalty)
Admission in Ninja Gram
Full screen
Console