Start learning free

LESSON 242 OF 675 · SETS

Every Letter in TypeScript: Checking a Pangram 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

    TYPESCRIPT · SETS

    Every Letter in TypeScript, explained

    Problem overview

    The method usesEveryLetter receives a string sentence made of lowercase English letters and spaces. It returns true when every letter from a to z appears at least once, and false otherwise. Spaces are not letters and do not count, and a letter may appear many times. The sentence has 1 to 1,000 characters and neither starts nor ends with a space.

    For "my jumpy dog fixed a big quiz with seven lucky parrots" all 26 letters appear, so the answer is true. For "six jumpy kittens quiz the brave fox and a wild hog" the letter c is missing, so the answer is false. For "abc abc abc" only three different letters appear, so the answer is false.

    Approach: count the different letters

    The question is not how many letters there are, but how many different ones. A Set counts different values by itself: it keeps each value once, however many times you add it. Put the sentence's letters in a set. If the set ends up with 26 entries, every letter is there.

    That works because the sentence contains only lowercase letters and spaces. Once the spaces are left out, the set can hold at most 26 different values, and 26 different values must be exactly the 26 letters.

    Step by step

    1. Create letters, an empty Set<string>. A character of a string is itself a string of length 1, so the set holds strings.
    2. Loop over the characters with for (let c of sentence). A string can be looped over directly with for...of.
    3. Skip the spaces with if (c != " "), so they stay out of the set.
    4. Add c to letters. A letter already there changes nothing.
    5. Return letters.size == 26. The comparison is already true or false, so it is returned as it is, without an if.

    The solution in TypeScript

    class Solution {
        usesEveryLetter(sentence: string): boolean {
            let letters: Set<string> = new Set();
            for (let c of sentence) {
                if (c != " ") {
                    letters.add(c);
                }
            }
            return letters.size == 26;
        }
    }
    

    Some details:

    • The space must stay out. new Set("abc abc abc") has 4 entries: a, b, c and the space. The loop skips the space, so the set has 3. A sentence with 25 letters and a space would otherwise reach a size of 26 and wrongly return true.
    • Looping over a string with for...of visits it one character at a time, and each character is a string.
    • Returning a comparison. letters.size == 26 is a boolean. The lesson's tests print true and false in lowercase.

    Another way

    You can remove the spaces first and build the set from what is left:

    function usesEveryLetter(sentence: string): boolean {
        return new Set(sentence.replaceAll(" ", "")).size === 26;
    }
    

    It gives true, false and false on the three examples. replaceAll returns a new string without spaces, and new Set accepts any iterable, a string included. The lesson's version spells out the loop so that each step is visible.

    Edge cases and common mistakes

    • Counting spaces. Without the c != " " test, a sentence that misses one letter but contains a space reaches size 26 and returns true by mistake.
    • Checking the sentence length. The length says nothing about the different letters: "abc abc abc" is 11 characters long and has only 3 different letters.
    • Exactly the alphabet. "abcdefghijklmnopqrstuvwxyz" returns true: the sentence may be only the 26 letters, with no space at all.

    Testing

    • "my jumpy dog fixed a big quiz with seven lucky parrots" gives true.
    • "six jumpy kittens quiz the brave fox and a wild hog" gives false.
    • "abc abc abc" gives false.
    • "abcdefghijklmnopqrstuvwxyz" gives true.
    Keep going with TypeScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free