Start learning free

LESSON 238 OF 673 · SETS

Every Letter in Python: A Pangram Check with a Set

Practice Read the full explanation

You get a string sentence made of lowercase letters and spaces.

Return True if every letter from a to z appears in it at least once. Otherwise, return False.

Spaces are not letters, so they don't count. A letter may appear many times.

 

Example 1:

Input: sentence = "my jumpy dog fixed a big quiz with seven lucky parrots"
Output: True
Explanation: All 26 letters appear at least once.

Example 2:

Input: sentence = "six jumpy kittens quiz the brave fox and a wild hog"
Output: False
Explanation: The letter c is missing.
The other 25 letters all appear.

Example 3:

Input: sentence = "abc abc abc"
Output: False
Explanation: Only 3 different letters appear: a, b and c.

 

Constraints:

  • sentence has 1 to 1,000 characters.
  • It has only lowercase English letters and spaces.
  • It has at least one letter and doesn't start or end with a space.
STEP 1 OF 5
    Ctrl + Enter to check

    PYTHON · SETS

    Every Letter in Python, explained

    Problem overview

    The method usesEveryLetter receives a string sentence of lowercase letters and spaces. It returns True if every letter from a to z appears at least once, and False otherwise. Spaces are not letters, and a letter may appear many times.

    "my jumpy dog fixed a big quiz with seven lucky parrots" returns True: all 26 letters appear. "six jumpy kittens quiz the brave fox and a wild hog" returns False, because the letter c is missing. "abc abc abc" returns False: it has only three different letters.

    Approach

    The question is how many different letters the sentence holds, not how often each appears. That is what a set is for. Put the letters in a set and repeats vanish. Since the sentence has only lowercase letters (once spaces are left out), there are 26 possible values, and the set has all of them exactly when its size is 26.

    Step by step

    1. Create an empty set letters = set().
    2. Loop over the characters with for c in sentence. Each c is a one-character string.
    3. Test c != " ", because a space is not a letter and must stay out of the set.
    4. Add c to letters.
    5. Return len(letters) == 26.

    The solution in Python

    class Solution:
        def usesEveryLetter(self, sentence: str) -> bool:
            letters = set()
            for c in sentence:
                if c != " ":
                    letters.add(c)
            return len(letters) == 26

    The comparison len(letters) == 26 is already True or False, so it is returned directly, without an if. Without the space check the set could hold up to 27 values, and a sentence missing a letter but containing a space could reach 26 and give a wrong True. The size test is valid because the input is guaranteed to hold only lowercase letters and spaces.

    Another way

    A string can be turned into a set directly. len(set(sentence.replace(" ", ""))) == 26 removes the spaces first and gives the same results on the three examples: True, False, False. The loop version is the one the lesson builds step by step.

    Reading the second example

    In "six jumpy kittens quiz the brave fox and a wild hog", the loop meets many repeated letters, such as i, t and a, and the set absorbs them. When the loop ends it holds 25 different letters: every letter except c. The test 25 == 26 is false, so the method returns False. In "abc abc abc" the set ends with just a, b and c, so the size is 3.

    Edge cases

    A sentence with many spaces and few letters, such as "a b c", simply has three letters in the set. A very short sentence returns False, since 26 different letters need at least 26 characters. The sentence is guaranteed to hold at least one letter and not to start or end with a space, so no special case is needed.

    Testing

    sentenceresult
    "my jumpy dog fixed a big quiz with seven lucky parrots"True
    "six jumpy kittens quiz the brave fox and a wild hog"False
    "abc abc abc"False

    Common mistakes

    • Letting spaces into the set. A space counts as a value, so a sentence with 25 different letters and a space has 26 values and wrongly passes.
    • Counting characters instead of different letters. A long sentence of repeated letters has many characters but few letters.
    • Using a list for letters. Then len counts repeats too, and the result is nearly always wrong.
    • Writing if len(letters) == 26: return True inside the loop without a final return. A sentence that never reaches 26 returns None, not False.
    Keep going with Python500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free