Check if a given string is Pangram in Java
Given string str, the task is to write Java Program check whether the given string is a pangram or not.
A string is a pangram string if it contains all the character of the alphabets ignoring the case of the alphabets.
Examples:
Input: str = “Abcdefghijklmnopqrstuvwxyz”
Output: Yes
Explanation: The given string contains all the letters from a to z (ignoring case).Input: str = “GeeksForGeeks”
Output: No
Explanation: The given string does not contain all the letters from a to z (ignoring case).
Method 1 – using a frequency array:
- Convert each letter of the string to the lower or upper case.
- Create a frequency array to mark the frequency of each letter from a to z.
- Then, traverse the frequency array and if there is any letter that is not present in the given string then print No, otherwise print Yes.
Below is the implementation of the above approach:
Java
// Java program for the above approach class GFG { static int size = 26 ; // Function to check if ch is a letter static boolean isLetter( char ch) { if (!Character.isLetter(ch)) return false ; return true ; } // Function to check if a string // contains all the letters from // a to z static boolean allLetter(String str, int len) { // Convert the given string // into lowercase str = str.toLowerCase(); // Create a frequency array to // mark the present letters boolean [] present = new boolean [size]; // Traverse for each character // of the string for ( int i = 0 ; i < len; i++) { // If the current character // is a letter if (isLetter(str.charAt(i))) { // Mark current letter as present int letter = str.charAt(i) - 'a' ; present[letter] = true ; } } // Traverse for every letter // from a to z for ( int i = 0 ; i < size; i++) { // If the current character // is not present in string // then return false, // otherwise return true if (!present[i]) return false ; } return true ; } // Driver Code public static void main(String args[]) { // Given string str String str = "Abcdefghijklmnopqrstuvwxyz" ; int len = str.length(); // Function Call if (allLetter(str, len)) System.out.println( "Yes" ); else System.out.println( "No" ); } } |
Yes
Time Complexity: O(N)
Auxiliary Space: O(26)
Method 2 – using Traversal: The idea is to convert the given string into lower case alphabets and then iterate over each character from a to z itself and check if the given string contains all the letters from a to z. If all the letters are present then print Yes, otherwise print No.
Below is the implementation of the above approach:
Java
// Java program for the above approach class GFG { // Function to check if a string // contains all the letters from // a to z (ignoring case) public static void allLetter(String str) { // Converting the given string // into lowercase str = str.toLowerCase(); boolean allLetterPresent = true ; // Loop over each character itself for ( char ch = 'a' ; ch <= 'z' ; ch++) { // Check if the string does not // contains all the letters if (!str.contains(String.valueOf(ch))) { allLetterPresent = false ; break ; } } // Check if all letter present then // print "Yes", else print "No" if (allLetterPresent) System.out.println( "Yes" ); else System.out.println( "No" ); } // Driver Code public static void main(String args[]) { // Given string str String str = "Abcdefghijklmnopqrstuvwxyz12" ; // Function call allLetter(str); } } |
Yes
Time Complexity: O(26*N)
Auxiliary Space: O(1)