We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Check if the Sentence Is Pangram

Number: 1960

Difficulty: Easy

Paid? No

Companies: Goldman Sachs, Bloomberg, Meta, Google, Adobe


Problem Description

Given a string containing only lowercase English letters, determine if the sentence is a pangram, meaning it contains every letter of the English alphabet at least once.


Key Insights

  • A pangram must include every letter from 'a' to 'z'.
  • Use a data structure (like a hash set) to keep track of unique letters encountered.
  • The check is successful if the size of the set is 26.
  • Early exit is possible as soon as all 26 letters have been found.

Space and Time Complexity

Time Complexity: O(n), where n is the length of the sentence. Space Complexity: O(1), since the set will store at most 26 characters regardless of input size.


Solution

The solution involves iterating over the characters of the sentence and adding each character to a hash set. Since there are only 26 letters in the English alphabet, if the set size reaches 26 at any point, you can immediately return true. After processing all characters, if the set size is still less than 26, return false. An alternative approach is to use a fixed-size boolean array of size 26 to mark the presence of each letter.


Code Solutions

# Function to check if a sentence is a pangram
def checkIfPangram(sentence):
    # Create a set to store unique characters
    seen = set()
    # Iterate over each character in the sentence
    for char in sentence:
        seen.add(char)  # Add the character to the set
        if len(seen) == 26:
            # Early return if all 26 letters are found
            return True
    # Return whether the set has all 26 letters
    return len(seen) == 26

# Example usage:
print(checkIfPangram("thequickbrownfoxjumpsoverthelazydog"))  # Expected output: True
← Back to All Questions