Leetcode – Check if the Sentence Is Pangram Solution

Spread the love

The problem “Check if the Sentence Is Pangram” on Leetcode is a captivating exercise that combines language processing and basic data structures. The problem gives programmers an opportunity to demonstrate their skills in Python, especially in the manipulation of strings and sets. This article will provide a comprehensive understanding of various approaches to solve this problem, while highlighting best practices and potential pitfalls.

Problem Statement

A pangram is a sentence where every letter of the English alphabet appears at least once. Given a string sentence, return True if the sentence is a pangram or False otherwise.


  • 1 ≤ ∣sentence∣ ≤1000
  • sentence consists of lowercase and uppercase English letters.


Input: sentence = "The quick brown fox jumps over the lazy dog"

Output: True

Approach 1: Brute-Force

The simplest approach involves creating a list of all the alphabets and then iterating over the sentence to remove the encountered letters from the list.

def checkIfPangram(sentence):
    alphabets = list("abcdefghijklmnopqrstuvwxyz")
    for char in sentence:
        if char.lower() in alphabets:
    return len(alphabets) == 0

Time Complexity

The time complexity is O(n×m), where n is the length of the sentence and m is the number of alphabets remaining to be found. In the worst-case, this can go up to 26.

Space Complexity

The space complexity is O(1) since the list of alphabets is of constant size.

Approach 2: Using Sets

A more Pythonic approach involves utilizing the set data structure. Convert the sentence into a set of its constituent letters and compare it with the set of all alphabets.

def checkIfPangram(sentence):
    alphabets = set("abcdefghijklmnopqrstuvwxyz")
    sentence_set = set(sentence.lower())
    return alphabets.issubset(sentence_set)

Time Complexity

The time complexity is O(n), where n is the length of the sentence.

Space Complexity

The space complexity is O(1) as the set sizes are constant.

Approach 3: Counting Uniques

This approach is about counting the unique alphabets present in the sentence and comparing that count with 26.

def checkIfPangram(sentence):
    return len(set(sentence.lower())) >= 26

Time Complexity

The time complexity is O(n), which is the same as the set-based approach.

Space Complexity

The space complexity is O(1).

Unit Testing

It’s advisable to validate the chosen approach with different test cases.

def test_checkPangram():
    assert checkIfPangram("The quick brown fox jumps over the lazy dog") == True
    assert checkIfPangram("Hello, World!") == False
    assert checkIfPangram("Python is amazing") == False
    print("All test cases pass")



The “Check if the Sentence Is Pangram” problem is a straightforward but educational problem that teaches important concepts in Python programming. It is versatile in that it can be approached in various ways, each with its pros and cons, teaching us valuable lessons in time and space complexity. Choosing the correct data structure and being aware of Python’s built-in capabilities can make the code more elegant and efficient.

Leave a Reply