algoadvance

A pangram is a sentence where every letter of the English alphabet appears at least once.

Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise.

Clarifying Questions

  1. Input Type: What is the type of the input?
    • The input is a string containing only lowercase English letters.
  2. Output Type: What is the type of the output?
    • The output is a boolean value, either True or False.
  3. Constraints:
    • The length of the sentence could be up to (10^4).
    • The string contains only lowercase English letters.

Strategy

  1. Use a Set for Uniqueness: Since we need each letter to appear at least once, we can utilize a set to keep track of distinct letters encountered in the string.
  2. Check Set Length: After processing the string, if the size of the set is 26 (the number of letters in the English alphabet), then the string is a pangram.
  3. Efficiency:
    • Insertion into a set and checking its length is efficient.
    • We traverse the string once, making the overall time complexity linear.

Code

def checkIfPangram(sentence: str) -> bool:
    # A set to track all unique letters in the sentence
    seen_letters = set()
    
    # Iterate through each character in the sentence
    for char in sentence:
        seen_letters.add(char)
    
    # Check if we have all 26 letters
    return len(seen_letters) == 26

# Example usage
sentence = "thequickbrownfoxjumpsoverthelazydog"
print(checkIfPangram(sentence))  # Output: True

Time Complexity

Thus, this solution is efficient and straightforward for checking if a sentence is a pangram.

Try our interview co-pilot at AlgoAdvance.com