Table of Contents
Problem Statement
In the “Pangram Checking” problem we have given a sentence “s”. Check if the given sentence/string is Pangram or not. A Pangram is a sentence/string containing every letter of the alphabet from a to z or No case sensitivity.
Input Format
The first and only one line containing a sentence/string “s”.
Output Format
The first and only one line containing “Yes” if the input string is a pangram. Otherwise, print “No”.
Constraints
- 1<=|s|<=10^6
- s[i] must be a lower case English alphabet or space ” “.
Example
pack my box with five dozen liquor jugs
Yes
pack my box with dozen liquor jugs
No
Algorithm
1. Create a hast table(bool vector). To mark the characters present in the string.
2. Traverse all characters in the input string.
3. If the uppercase character, subtract ‘A’ to find the index.
4. If the lower character, subtract ‘a’ to find the index.
5. Mark the value in the vector as true. (present)
6. Return false if any character is unmarked.
7. Else, return true.
Implementation
C++ Program
#include <bits/stdc++.h> using namespace std; int main() { string s; getline(cin, s); int n=s.length(); int freq[26]; memset(freq,0,sizeof(freq)); for(int i=0;i<n;i++) { if(s[i]!=' ') { freq[s[i]-'a']++; } } int temp=0; for(int i=0;i<26;i++) { if(freq[i]==0) { temp=1; break; } } if(temp==1) { cout<<"No"<<endl; } else { cout<<"Yes"<<endl; } return 0; }
Java Program
import java.util.HashMap; import java.util.Scanner; class sum { public static void main(String[] args) { Scanner sr = new Scanner(System.in); String s = sr.nextLine(); int n=s.length(); int freq[] = new int[26]; for(int i=0;i<26;i++) { freq[i]=0; } for(int i=0;i<n;i++) { if(s.charAt(i)!=' ') { freq[s.charAt(i)-'a']++; } } int temp=0; for(int i=0;i<26;i++) { if(freq[i]==0) { temp=1; break; } } if(temp==1) { System.out.println("No"); } else { System.out.println("Yes"); } } }
i really like the content of tutorialcup
No
Complexity Analysis
Time Complexity
O(n) where n is the size of the given string “s”. Here we simply visit the string char by char and store the freq of each char from a-z.
Space Complexity
O(1) because we don’t create too much extra space here. Simply declare an array of size 26 and store the freq of each char in it.