Problem

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:

1
2
3
Input: sentence = "thequickbrownfoxjumpsoverthelazydog"
Output: true
Explanation: sentence contains at least one of every letter of the English alphabet.

Example 2:

1
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

  1. Initialize an empty set to store unique letters.
  2. Iterate through each character in sentence and add it to the set.
  3. After processing, check if the set size is 26.
  4. Return True if all letters are present, otherwise False.

Complexity

  • ⏰ Time complexity: O(n), where n is the length of sentence, since we process each character once.
  • 🧺 Space complexity: O(1), since the set can contain at most 26 letters.

Code

1
2
3
4
5
6
7
8
9
class Solution {
    public boolean checkIfPangram(String sentence) {
        Set<Character> seen = new HashSet<>();
        for (char c : sentence.toCharArray()) {
            seen.add(c);
        }
        return seen.size() == 26;
    }
}
1
2
3
4
5
6
7
8
class Solution {
public:
    bool checkIfPangram(string sentence) {
        unordered_set<char> seen;
        for (char c : sentence) seen.insert(c);
        return seen.size() == 26;
    }
};
1
2
3
class Solution:
    def checkIfPangram(self, sentence: str) -> bool:
        return len(set(sentence)) == 26