Check if the Sentence Is Pangram in Java (Leetcode 1832): Strings
When you first see LeetCode 1832 (Check if the Sentence Is Pangram), your first thought might be to throw a HashSet at it or reach for a map. That works, but it's overkill. Because the English alphabet is locked at exactly 26 lowercase characters, we can solve this using a fixed 26-slot boolean checklist and simple ASCII subtraction.
In this post, I will walk you through how this mapping works under the hood, how to dry run it across test cases, and how to write the code in clean Java. I have also built an interactive letter-tracker widget right below so you can watch each letter flip from false to true as you step through a sentence.
Video Walkthrough
If you prefer watching me type out the solution live on LeetCode, fix quick syntax typos, and submit the code, watch the recording below:
1. What the Problem Asks
A pangram is a sentence where every single letter of the English alphabet appears at least once. It doesn't matter how often a letter shows up or what order they are in—as long as nothing from 'a' through 'z' is missing, it is a valid pangram.
- The input string only contains lowercase English letters. There are no spaces, punctuation marks, or uppercase characters to clean up.
- There are 26 letters in the alphabet. If the input sentence has fewer than 26 characters (
sentence.length() < 26), it cannot possibly be a pangram. - Return
trueif every letter fromatozis present, otherwise returnfalse.
The Core Idea: ASCII Offset
In Java, characters are backed by integer ASCII values. Lowercase 'a' has an ASCII code of 97, 'b' is 98, and 'z' is 122. If you subtract 'a' from any lowercase letter, you cleanly shift the character into an array index from 0 to 25:
97 - 97 Index 0
98 - 97 Index 1
99 - 97 Index 2
122 - 97 Index 25
By checking off arr[char - 'a'] = true for every character in the string, we turn the problem into a simple 26-box checklist.
Quick Examples
Output: true
Why: All 26 letters appear in the sentence at least once.
Output: false
Why: Only letters 'c', 'd', 'e', 'l', 'o', 't' are used. Letters like 'a', 'b', and 'z' are missing.
2. Interactive 26-Letter Tracker
Click through the live tracker below to see how our boolean array populates step-by-step as each letter is scanned.
3. Step-by-Step Code Walkthrough
Here is how the algorithm executes in code:
- Allocate a fixed array: We declare
boolean[] arr = new boolean[26];. In Java, all elements in a newly created boolean array automatically default tofalse. - Traverse each character: We loop from
0tosentence.length() - 1. For each character, we compute its slot withsentence.charAt(i) - 'a'and set that index totrue. - Verify all 26 slots: Finally, we loop from index
0to25. If any slot remainsfalse, we can immediately returnfalse. If the loop completes without issue, every letter was visited, and we returntrue.
The Java Solution
class Solution {
public boolean checkIfPangram(String sentence) {
// Quick optimization: a pangram needs at least 26 characters
if (sentence.length() < 26) {
return false;
}
boolean[] arr = new boolean[26];
// Step 1: Mark each character found
for (int i = 0; i < sentence.length(); i++) {
arr[sentence.charAt(i) - 'a'] = true;
}
// Step 2: Check if any alphabet letter was missed
for (int i = 0; i < 26; i++) {
if (arr[i] == false) {
return false;
}
}
return true;
}
}
Test It Locally
To run multiple test cases quickly in your local IDE, use this Main class:
public class Main {
public static boolean checkIfPangram(String sentence) {
if (sentence.length() < 26) return false;
boolean[] arr = new boolean[26];
for (int i = 0; i < sentence.length(); i++) {
arr[sentence.charAt(i) - 'a'] = true;
}
for (int i = 0; i < 26; i++) {
if (!arr[i]) return false;
}
return true;
}
public static void main(String[] args) {
String[] testCases = {
"thequickbrownfoxjumpsoverthelazydog",
"leetcode",
"packmyboxwithfivedozenliquorjugs",
"abcdefghijklmnopqrstuvwxy" // 25 chars, missing 'z'
};
for (String test : testCases) {
System.out.println("\"" + test + "\" -> " + checkIfPangram(test));
}
}
}
Console Output:
"leetcode" -> false
"packmyboxwithfivedozenliquorjugs" -> true
"abcdefghijklmnopqrstuvwxy" -> false
4. Complexity Analysis
- Time Complexity:
O(n), wherenis the length of the string. We iterate through the string once to set the booleans, and then run a constant 26-step loop at the end. - Space Complexity:
O(1). The boolean array is permanently sized at 26 elements regardless of whether the input has 30 characters or 10,000 characters.
5. Traps & Interview Tips
- Pangram vs. Anagram: Don't mix up the two terms during an interview. An anagram requires two strings to have the exact same character frequencies. A pangram only checks if a single sentence contains all 26 alphabet letters.
- String length syntax: In Java, arrays use
arr.length, but strings use the method callsentence.length(). Forgetting the parentheses is an easy typo to make while coding under pressure. - Alternative bitmask approach: If an interviewer asks if you can solve this without any extra array allocation at all, mention that you can use a single 32-bit integer as a bitmask (setting the
k-th bit withmask |= (1 << (ch - 'a'))) and checking ifmask == (1 << 26) - 1. - Next problems to practice:
- LeetCode 242 (Valid Anagram): Similar fixed-size frequency counting approach.
- LeetCode 387 (First Unique Character in a String): Practice using character frequency arrays.
Comments
Post a Comment