Skip to content
← All posts

Check if the Sentence Is Pangram

5 min read
leetcodeproblemeasystringshash-map

You are given a string sentence containing only lowercase English letters. Return true if sentence is a pangram, meaning it contains every letter of the English alphabet at least once. Otherwise, return false.

This is LeetCode 1832: Check if the Sentence Is Pangram, and it is one of the cleanest problems for learning how sets track unique elements. The idea behind it appears constantly in string problems, from checking character coverage to validating constraints on input.

abcdefghijklmnopqrstuvwxyz
Green = letter found in the sentence, dark = letter missing. A pangram lights up all 26 cells.

Why this problem matters

Using a set to track which elements you have seen is one of the most foundational patterns in programming. You will reach for it in problems about duplicates, frequency counting, membership testing, and coverage checks. Pangram checking is the simplest version of that pattern: "have I seen all 26 letters?" Once you can answer that question with a set, you can adapt the same approach to harder problems like finding missing characters, verifying anagram conditions, or checking substring coverage.

The key insight

You do not need to count how many times each letter appears. You only need to know whether each letter appears at least once. A set gives you exactly that. Add every character in the sentence to a set, and when you are done, check if the set contains all 26 letters. If the set's size is 26, every letter of the alphabet showed up at least once, so the sentence is a pangram.

The solution

def check_if_pangram(sentence: str) -> bool:
    return len(set(sentence)) == 26

That one-liner does everything. Let's break it down.

set(sentence) creates a set from the characters in the string. Because a set only stores unique elements, duplicates are automatically discarded. If the sentence is "aab", the set becomes {'a', 'b'}, not {'a', 'a', 'b'}.

len(...) gives us the number of unique characters. Since the input contains only lowercase English letters, the maximum possible set size is 26. If the length equals 26, every letter from a through z appeared at least once.

You could also write this more explicitly with a loop:

def check_if_pangram(sentence: str) -> bool:
    seen = set()
    for ch in sentence:
        seen.add(ch)
    return len(seen) == 26

Both versions do the same thing. The one-liner is more Pythonic, but the explicit loop makes the logic visible if you are explaining it in an interview.

Whenever a problem asks "does the input contain all of X?" or "are any elements missing?", a set is usually the right first tool to reach for. Sets give you O(1) lookups and automatically handle duplicates, which makes coverage checks trivial.

Visual walkthrough

Let's trace through a short example, "abcxyz", character by character. Watch how the set grows as we scan each letter.

Start: empty set, begin scanning the sentence "abcxyz"

abcxyz
{ }

Set size: 0. We need 26 for a pangram.

Step 1: Read 'a'. It is new, add it to the set.

abcxyz
a

Set: {a}. Size: 1.

Step 2: Read 'b'. It is new, add it to the set.

abcxyz
ab

Set: {a, b}. Size: 2.

Step 3: Read 'c'. It is new, add it to the set.

abcxyz
abc

Set: {a, b, c}. Size: 3.

Step 4: Read 'x'. It is new, add it to the set.

abcxyz
abcx

Set: {a, b, c, x}. Size: 4.

Step 5: Read 'y'. It is new, add it to the set.

abcxyz
abcxy

Set: {a, b, c, x, y}. Size: 5.

Step 6: Read 'z'. It is new, add it to the set.

abcxyz
abcxyz

Set: {a, b, c, x, y, z}. Size: 6. Done scanning.

Result: Set size is 6, not 26. This is NOT a pangram.

abcxyz

6 unique letters out of 26 needed. Return false.

After scanning all six characters, the set contains only 6 unique letters. Since 6 is not 26, this sentence is not a pangram. If we had used a full pangram like "thequickbrownfoxjumpsoverthelazydog", the set would have grown to size 26, and we would return true.

Complexity analysis

ApproachTimeSpace
SetO(n)O(1)

Time is O(n) where n is the length of the sentence. We iterate through every character once to build the set. Each insertion into the set is O(1) on average.

Space is O(1), not O(n). This might surprise you at first. The set can hold at most 26 entries because the input only contains lowercase English letters. No matter how long the sentence is, the set never exceeds 26 elements. That is a fixed upper bound, so the space is constant.

The building blocks

1. Set for unique element tracking

The core pattern is using a set to answer "which distinct elements have I seen?"

seen = set()
for item in collection:
    seen.add(item)
# seen now contains only unique items

You will use this same pattern in problems like Two Sum (checking if a complement exists), Contains Duplicate (checking if any element repeats), and Longest Substring Without Repeating Characters (tracking the current window's characters).

2. Alphabet coverage check

The pattern of verifying that all letters from a to z are present:

return len(seen) == 26

This specific check applies to pangram problems, but the general idea of comparing a set's size to an expected count is universal. You might check len(seen) == k for some target, or compare seen against a reference set. The underlying technique is always the same: collect uniques, then verify coverage.

Edge cases

Before submitting, think through these scenarios:

  • Exactly 26 characters, all unique: the shortest possible pangram. Each letter appears exactly once. Return true.
  • Very long sentence with all 26 letters: duplicates do not matter. The set still reaches size 26. Return true.
  • Missing just one letter: the set has 25 elements. Return false. This is easy to miss if you test only with obvious pangrams.
  • Single character: "a" gives a set of size 1. Return false.
  • All same character: "aaaaaaa" gives a set of size 1. Return false.

From understanding to recall

You have seen how a set solves the pangram problem: add each character, check if the set size hits 26. The logic is simple, and the code is short. But under interview pressure, even simple problems can trip you up if you have not practiced writing them from memory.

The details that matter are small. Do you use a set or a dictionary? Do you check len(seen) == 26 or len(seen) >= 26? Do you need to handle uppercase letters or special characters? These are not hard questions, but they cost time if you are figuring them out on the spot instead of recalling them instantly.

Spaced repetition turns understanding into automatic recall. You practice building the set, writing the coverage check, and handling edge cases at increasing intervals. After a few rounds, the pattern is second nature. You see "check if all letters are present" and the code writes itself.

Related posts

CodeBricks breaks Check if the Sentence Is Pangram into its set-based tracking and alphabet coverage building blocks, then drills them independently with spaced repetition. You type each piece from scratch until the pattern is automatic. When a string coverage problem shows up in your interview, you do not hesitate. You just write it.