Check if the Sentence Is Pangram
EasyUpdated: Jul 27, 2025
Practice on:
Problem
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.
Examples
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
Solution
Method 1 – Hash Set for Unique Letters
Intuition
A pangram must contain every letter from 'a' to 'z' at least once. By adding each character in the sentence to a set, we can easily check if all 26 letters are present.
Approach
- Initialize an empty set to store unique letters.
- Iterate through each character in
sentenceand add it to the set. - After processing, check if the set size is 26.
- Return
Trueif all letters are present, otherwiseFalse.
Complexity
- ⏰ Time complexity:
O(n), wherenis the length ofsentence, since we process each character once. - 🧺 Space complexity:
O(1), since the set can contain at most 26 letters.
Code
Java
class Solution {
public boolean checkIfPangram(String sentence) {
Set<Character> seen = new HashSet<>();
for (char c : sentence.toCharArray()) {
seen.add(c);
}
return seen.size() == 26;
}
}
C++
class Solution {
public:
bool checkIfPangram(string sentence) {
unordered_set<char> seen;
for (char c : sentence) seen.insert(c);
return seen.size() == 26;
}
};
Python
class Solution:
def checkIfPangram(self, sentence: str) -> bool:
return len(set(sentence)) == 26