Check If Sentence Is Pangram

hash-table string

Description

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.

Example 1:
Input: sentence = "thequickbrownfoxjumpsoverthelazydog"
Output: true
Explanation: sentence contains at least one of every letter of the English alphabet.

Example 2:
Input: sentence = "leetcode"
Output: false
Explanation: sentence is missing letters like 'a', 'b', 'f', 'g', etc.

Example 3:
Input: sentence = "abcdefghijklmnopqrstuvwxyz"
Output: true
Explanation: sentence contains exactly one of each letter of the alphabet.

Constraints

  • 1 <= sentence.length <= 1000
  • sentence consists of lowercase English letters

Complexity

Show Complexity
  • Time: O(n)
  • Space: O(1)

Hints

Show Hints
Pattern
Hash set to track unique characters
Approach
Add each character to a set, then check if set size equals 26
Complexity
O(1) space since at most 26 letters

Solutions

Show PY Solution
def solution(sentence: str) -> bool:
    s = set(sentence)
    return len(s) == 26

    # s = set(sentence)
    #
    # for i in range(26):
    #     alph = chr(ord("a") + i)
    #     if alph not in s:
    #         return False
    #
    # return True

    # m = {}
    #
    # for i in range(len(sentence)):
    #     c = sentence[i]
    #     m[c] = 1
    #
    # for i in range(26):
    #     alph = chr(ord('a') + i)
    #     if alph not in m:
    #         return False
    #
    # return True