Check if the Sentence Is Pangram (LeetCode 1832): Solution and Explanation

Easy
Hash table
String
Bit manipulation

Given a lowercase string, decide whether every letter from a to z appears in it at least once.

Intuition

A pangram only cares whether each letter shows up, never how often or where. So the whole sentence collapses into a yes/no checklist of 26 letters.

Any set structure works as that checklist, but 26 yes/no answers fit in the low 26 bits of a single integer. Seeing letter k means seen |= 1 << k; seeing it again changes nothing.

The sentence is a pangram exactly when all 26 bits are on, which is the number (1 << 26) - 1. Checking that after every character also lets you stop early, as soon as the last missing letter appears.

Approach: Bitmask checklist with early exit

  1. Set seen = 0.
  2. For each character ch, set bit ch - 'a' in seen.
  3. If seen == (1 << 26) - 1, return true immediately.
  4. If the loop finishes without filling the mask, return false.

Step-by-step example

sentence = "thequickbrownfoxjumpsoverthelazydog"
  1. t, h, e, q, u, i, c, k are all new, so eight bits turn on.
  2. b, r, o, w, n, f, x, j, m, p, s, v keep adding new bits. Repeats such as the second o and u change nothing.
  3. l, a, z, y add four more, leaving only d and g missing.
  4. d turns on bit 3 and finally g (the last character) turns on bit 6. The mask now equals (1 << 26) - 1, so return true.

Code: Python, Java, C++

1class Solution:
2    def checkIfPangram(self, sentence: str) -> bool:
3        seen = 0
4        for ch in sentence:
5            seen |= 1 << (ord(ch) - 97)
6            if seen == (1 << 26) - 1:
7                return True
8        return False
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each character is read once with a constant-time bit operation, and the scan may stop early.

Space

O(1)

One integer holds the whole checklist.

Other approaches

Set size (time O(n), space O(1))

Return len(set(sentence)) == 26. It is the shortest answer in Python and fine at this input size, but it always reads the whole string and allocates a set.

Boolean array plus counter (time O(n), space O(1))

Keep bool[26] and a counter of distinct letters, incrementing it when a slot flips from false to true. Same early exit as the bitmask, a little more code.

Search for each letter (time O(26 n), space O(1))

For each of the 26 letters, check whether it occurs in the sentence. Simple, and still linear because 26 is a constant, but it reads the string up to 26 times.

Common mistakes

  • Returning true because the sentence has at least 26 characters. Length says nothing about distinct letters, as "aaaa..." shows.
  • Counting characters rather than distinct letters, so repeats push the count to 26 too early.
  • Comparing the mask with 1 << 26 instead of (1 << 26) - 1. The full mask is 26 ones, not a single bit 26.

Frequently asked questions

Why use a bitmask instead of a set?

Both are correct. The bitmask needs no allocation, each update is one OR, and the full check is a single integer comparison. It is a common follow-up when the alphabet is small.

Can a sentence shorter than 26 characters be a pangram?

No. It needs at least 26 characters to contain 26 distinct letters, so you could return false early for short inputs, although the loop gives the same answer.

Does the early exit change the complexity?

Not in the worst case, which is still O(n), but it helps whenever the last missing letter appears before the end of the sentence.

What is the time and space complexity?

O(n) time and O(1) space.