Watch 10 video solutions for Check if the Sentence Is Pangram, a easy level problem involving Hash Table, String. This walkthrough by codestorywithMIK has 21,612 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 1000sentence consists of lowercase English letters.Problem Overview: You receive a lowercase sentence and need to determine whether it is a pangram—a string that contains every letter from 'a' to 'z' at least once. The task is essentially checking if all 26 English alphabet characters appear in the string.
Approach 1: Using a Set to Track Letters (O(n) time, O(1) space)
The most straightforward method uses a set (or hash set) to track unique characters encountered while iterating through the sentence. As you scan each character, insert it into the set. Since sets automatically discard duplicates, the structure eventually holds only unique letters. After processing the entire string, check whether the set size equals 26. This approach relies on constant-time hash insertions and lookups, making the scan linear with respect to the sentence length.
This technique is a classic application of a hash table for uniqueness tracking. It’s concise, readable, and widely used in interviews because it directly maps the problem requirement (unique characters) to the right data structure.
Approach 2: Boolean Array for Tracking (O(n) time, O(1) space)
A slightly more memory-efficient method replaces the set with a fixed-size boolean array of length 26. Each index corresponds to a letter: index = char - 'a'. Iterate through the sentence and mark the corresponding index as true when a character appears. After the scan, verify that every position in the array is marked true. If any entry remains false, the sentence is missing that letter.
This approach avoids hashing and uses direct indexing instead. The memory footprint is constant because the array size never exceeds 26. It also tends to run slightly faster in lower-level languages due to predictable memory access patterns. The logic is simple: iterate through the string, mark presence, then verify coverage of the alphabet.
Some implementations optimize further by maintaining a counter of discovered letters. Each time a new letter is marked for the first time, increment the counter. Once it reaches 26, you can terminate early without scanning the rest of the string.
Recommended for interviews: Both solutions run in O(n) time and O(1) space, but the boolean array approach demonstrates stronger understanding of constraints and constant-space optimization. Interviewers often expect the hash set solution first because it’s the most intuitive. Following it with the boolean array optimization shows you can reduce overhead when the input domain (26 letters) is fixed.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Hash Set to Track Letters | O(n) | O(1) | General case; simplest and most readable approach using a hash set |
| Boolean Array (26 Letters) | O(n) | O(1) | When optimizing for minimal overhead and fixed alphabet size |