Check if the Sentence Is Pangram
EasyPrompt
A pangram is a sentence where every letter of the English alphabet appears at least once.
Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise.
Example 1:
Input: sentence = "thequickbrownfoxjumpsoverthelazydog"
Output: true
Explanation: sentence contains at least one of every letter of the English alphabet.Example 2:
Input: sentence = "leetcode"
Output: false
Constraints:
1 <= sentence.length <= 1000sentenceconsists of lowercase English letters.
Approaches
4 approaches with complexity analysis and trade-offs.
This straightforward approach checks the condition for a pangram directly. It verifies the presence of every single letter of the alphabet, one by one, by searching for it within the given sentence. If it finds that even one letter is missing, it stops and returns false. Only if all 26 letters are found does it confirm the sentence is a pangram.
Algorithm
- Iterate through all 26 lowercase English letters from 'a' to 'z'.
- For each letter, search the entire input
sentenceto check for its presence. - A common way to search is using
sentence.indexOf(char)which returns -1 if the character is not found. - If any character is not found in the sentence, we can immediately conclude it's not a pangram and return
false. - If the loop completes without finding any missing character, it means all 26 letters are present, and we return
true.
Walkthrough
The core idea is to verify the presence of all 26 required characters one by one.
We loop from 'a' to 'z'. In each iteration, we take one letter and scan the entire input sentence to find it. Java's String.indexOf() method is well-suited for this search; it returns the index of the first occurrence of a character or -1 if the character is not present.
If at any point indexOf() returns -1, we know a letter from the alphabet is not in the sentence. We can then conclude that the sentence is not a pangram and return false immediately, without checking the remaining letters.
If the loop completes successfully without returning false, it implies that every letter from 'a' to 'z' was found in the sentence, making it a pangram. We then return true.
class Solution { public boolean checkIfPangram(String sentence) { // Iterate through all 26 lowercase letters. for (char c = 'a'; c <= 'z'; c++) { // Check if the sentence contains the current character. // indexOf returns -1 if the character is not found. if (sentence.indexOf(c) == -1) { // If any character is missing, it's not a pangram. return false; } } // If the loop completes, all characters were found. return true; }}Complexity
Time
O(N). Although the asymptotic complexity is linear, it's practically O(26 * N), where N is the length of the sentence. The outer loop runs a constant 26 times, and inside, `indexOf()` can take up to O(N) time. This makes it slower than single-pass approaches.
Space
O(1), as no additional space that scales with the input size is allocated.
Trade-offs
Pros
The logic is very simple and easy to understand.
It uses O(1) extra space.
Cons
This approach is inefficient because it repeatedly scans the input string. For a sentence of length N, it performs up to 26 full scans, leading to a high constant factor in its time complexity.
It does not take advantage of more efficient data structures for presence checking.
Solutions
Solution
class Solution {public boolean checkIfPangram(String sentence) { boolean[] vis = new boolean[26]; for (int i = 0; i < sentence.length(); ++i) { vis[sentence.charAt(i) - 'a'] = true; } for (boolean v : vis) { if (!v) { return false; } } return true; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.