Start learning free

LESSON 243 OF 678 · SETS

Every Letter in Swift: A Set of Characters

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

    SWIFT · SETS

    Every Letter in Swift, explained

    Problem overview

    The function usesEveryLetter receives a string sentence made 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.

    The sentence "my jumpy dog fixed a big quiz with seven lucky parrots" returns true, because all 26 letters appear. The sentence "six jumpy kittens quiz the brave fox and a wild hog" returns false: the letter c is missing. "abc abc abc" returns false because only 3 different letters appear.

    Approach: collect the different letters

    The question is how many different letters the sentence has, and a set keeps each value once, however many times it is inserted. Insert every letter of the sentence, and the set ends up holding exactly the distinct letters. If there are 26 of them and the text only has lowercase letters, then none of the 26 is missing.

    The one thing to keep out of the set is the space. Otherwise a sentence with 25 letters and some spaces would reach a count of 26 by accident.

    Step by step

    1. Create letters, an empty Set<Character>() with var. Swift's Character is one character of a string.
    2. Loop over the string with for c in sentence. Looping over a String in Swift gives its characters one by one.
    3. Skip spaces with if c != " ".
    4. Insert the letter. A letter already in the set changes nothing.
    5. Return letters.count == 26. The comparison is already true or false, so return it as it is.

    The solution in Swift

    class Solution {
        func usesEveryLetter(_ sentence: String) -> Bool {
            var letters = Set<Character>()
            for c in sentence {
                if c != " " {
                    letters.insert(c)
                }
            }
            return letters.count == 26
        }
    }
    

    Swift details: in c != " ", the literal " " is read as a Character, because c is one. A String can't be indexed with a number in Swift, so looping with for c in sentence is the simplest way to visit its characters.

    Why 26 is enough

    The set can only hold lowercase English letters, since that is all the sentence contains besides spaces, and there are exactly 26 of those. A set of 26 different values taken from 26 possible ones must contain all of them. With the input rules the lesson sets, no separate check for each letter is needed.

    What the space does

    Run on "abc abc abc", the set holds 3 different letters. If the space check is left out, the same text gives a set of 4 characters: a, b, c and the space. With 25 letters plus a space, that gap would be enough to reach 26 wrongly, which is why the space is skipped.

    Testing

    The three examples return:

    true
    false
    false
    

    Common mistakes

    • Counting the space. Without the if, a text with 25 distinct letters and a space has 26 different characters, and the function wrongly returns true.
    • Counting characters instead of different ones. sentence.count is the length of the text. Only the set's count counts different letters.
    • Using an array instead of a set. An array keeps every repeat, so its count is the length of the text.
    • Adding letters from a longer text. The rule that the text has only lowercase letters and spaces is what makes 26 the right count. If capitals were allowed, A and a would be two different characters in the set.
    Keep going with Swift500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free