Start learning free

LESSON 242 OF 671 · SETS

Every Letter in Go: Pangram Check with a Set of Runes

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

    GO · SETS

    Every Letter in Go, explained

    Problem overview

    usesEveryLetter receives a string sentence of lowercase letters and spaces and returns true when each letter from a to z appears at least once, otherwise false. Spaces don't count as letters, and a letter may appear many times.

    The sentence "my jumpy dog fixed a big quiz with seven lucky parrots" has all 26 letters. "six jumpy kittens quiz the brave fox and a wild hog" lacks c. "abc abc abc" has only three different letters.

    Approach

    You don't need to count how often each letter appears, only which letters appear. That is what a set is for: it stores each value once, however many times you add it. Collect the letters of the sentence in a set, then check whether it holds 26 of them.

    The input has only lowercase letters and spaces. If spaces are kept out of the set, every key is one of the 26 letters, so 26 different keys means none is missing.

    Go has no set type, so the set is a map with bool values. Ranging over a string yields rune values (one per character), so the keys are runes: map[rune]bool.

    Step by step

    1. Create letters := map[rune]bool{}.
    2. Loop with for _, c := range sentence. The first value, the byte position, goes into _.
    3. If c != ' ', set letters[c] = true. Adding a letter that is already there changes nothing.
    4. Return len(letters) == 26. The comparison is already a bool, so it is returned as it is.

    The solution in Go

    func usesEveryLetter(sentence string) bool {
        letters := map[rune]bool{}
        for _, c := range sentence {
            if c != ' ' {
                letters[c] = true
            }
        }
        return len(letters) == 26
    }
    

    Go details worth knowing:

    • A single-quoted ' ' is a rune literal, which is what c is compared with. A double-quoted " " would be a string and would not compile in that comparison.
    • Indexing a string, as in sentence[i], gives a byte. Ranging over it gives runes, which is the better fit when you care about characters. For this lesson's ASCII input both would work.

    Take "abc abc abc". The sentence has 11 characters, but the set receives only a, b and c, so len(letters) is 3 and the result is false. If the space were added too, the set would hold 4 entries. That is why the space check comes first: otherwise a sentence with 25 letters and a space would reach 26 entries and wrongly pass.

    Testing

    "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
    

    The tests print the booleans in lowercase, as above.

    Because the set stores each letter once, a long sentence full of repeated letters costs no more than a short one with the same letters. The function reads each character a single time, so the work grows with the length of the sentence.

    Common mistakes

    • Adding the space to the set. The set could then reach 26 entries with only 25 letters present. Skip spaces.
    • Counting characters instead of different letters. A long sentence has many characters; the check is about distinct ones.
    • Comparing with 25 or 27. The alphabet has 26 letters, and the set holds no space.
    Keep going with Go500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free