Java String Program · S28
Check If a String Is a Pangram in Java
A pangram uses every letter of the alphabet at least once. Track which of the 26 letters you have seen and stop as soon as the last one appears.
Problem Statement
Write a method that returns true if a sentence contains every English letter from a to z at least once, ignoring case.
Digits, spaces and punctuation are ignored. An empty string is not a pangram.
Input/Output Examples
"Pack my box with five dozen liquor jugs"OutputtrueExplanation: all 26 letters appear at least once."Hello World"OutputfalseExplanation: only 7 different letters are used.""OutputfalseExplanation: no letters at all.Visual Explanation
true immediately.A letter that has already been seen does not change anything, so the distinct counter grows only when a new letter appears. That counter is what allows an early exit without checking the whole array.
Approaches
Both approaches lowercase each character and consider only a to z. The first can stop early.
Approach 1: boolean[26] With Early Exit (Recommended)
Idea: mark each letter in a boolean[26] and count how many different letters have been marked.
- Lowercase each character and skip anything outside
a–z. - If the letter is not marked yet, mark it and increase
distinct. - Return
trueas soon asdistinctreaches 26; otherwise returnfalse.
Time: O(n) · Space: O(1).
Approach 2: Streams distinct()
Idea: stream the characters, keep lowercase letters, remove duplicates and check that 26 remain.
- Map each character with
Character::toLowerCase. - Keep only
a–z, then calldistinct(). - Compare
count()with 26.
Time: O(n) · Space: O(1). distinct() stores at most 26 values.
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: boolean[26] With Early Exit (Recommended)
For long texts that are pangrams, the loop usually stops well before the end.
public class PangramBoolean {
// Marks each letter once and stops as soon as all 26 have been seen
static boolean isPangram(String text) {
boolean[] seen = new boolean[26];
int distinct = 0;
for (int i = 0; i < text.length(); i++) {
char c = Character.toLowerCase(text.charAt(i));
if (c >= 'a' && c <= 'z' && !seen[c - 'a']) {
seen[c - 'a'] = true;
if (++distinct == 26) {
return true;
}
}
}
return false;
}
public static void main(String[] args) {
String[] inputs = {"Pack my box with five dozen liquor jugs", "Hello World", ""};
for (String s : inputs) {
System.out.println("\"" + s + "\" -> " + isPangram(s));
}
}
}
"Pack my box with five dozen liquor jugs" -> true "Hello World" -> false "" -> false
Time complexity: O(n) · Space complexity: O(1).
Approach 2: Streams distinct()
Very readable, but it always processes the whole string.
public class PangramStream {
// Counts the distinct lowercase letters and compares the count with 26
static boolean isPangram(String text) {
return text.chars()
.map(Character::toLowerCase)
.filter(c -> c >= 'a' && c <= 'z')
.distinct()
.count() == 26;
}
public static void main(String[] args) {
String[] inputs = {"Pack my box with five dozen liquor jugs", "Hello World", ""};
for (String s : inputs) {
System.out.println("\"" + s + "\" -> " + isPangram(s));
}
}
}
"Pack my box with five dozen liquor jugs" -> true "Hello World" -> false "" -> false
Time complexity: O(n) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| boolean[26] With Early Exit | O(n) | O(1) |
| Streams distinct() | O(n) | O(1) |
Here n is the length of the string. Both approaches read each character at most once and remember at most 26 letters.
Interview Tips and Recommended Approach
- Ignore non-letters explicitly. Counting distinct characters without a filter would let spaces and digits fill the 26 slots.
- Mention the bitmask variant. An
intwith 26 bits can replace the array: the string is a pangram when the mask equals(1 << 26) - 1. - Add a quick length check. A string shorter than 26 characters cannot be a pangram.
- Ask about other alphabets. Accented or non-English letters need a different definition of "every letter".
Recommended Approach: boolean[26] With Early Exit
Why: it uses a fixed 26-slot array and stops as soon as the answer is known, which makes it both memory-light and fast on real text.