LeetCode Problem Workspace

Check if the Sentence Is Pangram

Determine if a given lowercase string contains every English letter using efficient hash table tracking techniques.

category

2

Topics

code_blocks

7

Code langs

hub

3

Related

Practice Focus

Easy · Hash Table plus String

bolt

Answer-first summary

Determine if a given lowercase string contains every English letter using efficient hash table tracking techniques.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Hash Table plus String

Try AiBox Copilotarrow_forward

This problem asks whether a string is a pangram, meaning it contains all 26 English letters at least once. The solution uses a hash table or a fixed-size boolean array to track seen letters while iterating through the string. It returns true if all letters are found, false otherwise, optimizing both time and space for up to 1000 characters.

Problem Statement

You are given a string containing only lowercase English letters. A pangram is defined as a sentence where every letter of the alphabet appears at least once. Write a function that returns true if the input string is a pangram and false otherwise.

For example, the sentence "thequickbrownfoxjumpsoverthelazydog" contains all English letters and should return true, while "leetcode" does not and should return false. Constraints: the string length is between 1 and 1000 characters, and it contains only lowercase English letters.

Examples

Example 1

Input: sentence = "thequickbrownfoxjumpsoverthelazydog"

Output: true

sentence contains at least one of every letter of the English alphabet.

Example 2

Input: sentence = "leetcode"

Output: false

Example details omitted.

Constraints

  • 1 <= sentence.length <= 1000
  • sentence consists of lowercase English letters.

Solution Approach

Boolean Array Tracking

Initialize a boolean array of size 26 for each letter. Iterate through the string, marking the corresponding index as true for each letter. Return true if all entries in the array are true, false otherwise.

Hash Set Method

Use a hash set to store characters as you iterate through the string. If the set reaches a size of 26, return true early. Otherwise, return false after processing all characters.

Bitmask Optimization

Represent each letter as a bit in a 26-bit integer. For each character, set the corresponding bit. After processing the string, check if all 26 bits are set, indicating a pangram.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(n) where n is the length of the string, as each character is processed once. Space complexity can be O(1) with a fixed-size array or bitmask, or O(26) for a hash set, which is effectively constant.

What Interviewers Usually Probe

  • Checking if the candidate correctly identifies the pattern as hash table plus string.
  • Looking for early exit strategies when all letters are found.
  • Observing how the candidate handles space-efficient tracking using boolean arrays or bitmasks.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting that only lowercase letters are present and using an oversized array.
  • Not accounting for repeated letters, leading to incorrect counting logic.
  • Iterating the array or set after every insertion instead of checking only at the end or when full.

Follow-up variants

  • Determine if a string contains all vowels using a similar hash table approach.
  • Check if a string contains all letters from a custom alphabet pattern.
  • Verify pangram status ignoring non-letter characters in a mixed string.

FAQ

What does 'Check if the Sentence Is Pangram' mean in coding terms?

It means determining if a lowercase string contains all 26 English letters at least once.

Can I use a set or dictionary for this problem?

Yes, a hash set works well to track seen letters while iterating through the string.

Is there a more space-efficient method than a boolean array?

Yes, using a 26-bit bitmask can represent all letters using a single integer.

How does early exit improve performance?

You can return true as soon as all 26 letters are found, avoiding unnecessary iteration.

What is the core pattern to recognize in this problem?

This problem follows the hash table plus string pattern, where tracking characters efficiently is key.

terminal

Solution

Solution 1: Array or Hash Table

Traverse the string `sentence`, use an array or hash table to record the letters that have appeared, and finally check whether there are $26$ letters in the array or hash table.

1
2
3
class Solution:
    def checkIfPangram(self, sentence: str) -> bool:
        return len(set(sentence)) == 26

Solution 2: Bit Manipulation

We can also use an integer $mask$ to record the letters that have appeared, where the $i$-th bit of $mask$ indicates whether the $i$-th letter has appeared.

1
2
3
class Solution:
    def checkIfPangram(self, sentence: str) -> bool:
        return len(set(sentence)) == 26
Check if the Sentence Is Pangram Solution: Hash Table plus String | LeetCode #1832 Easy