Last Updated : 15 Jul, 2025
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:
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");
}
}
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);
}
}
Time Complexity: O(26*N)
Auxiliary Space: O(1)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4