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
- Create
letters, an emptySet<Character>()withvar. Swift'sCharacteris one character of a string. - Loop over the string with
for c in sentence. Looping over aStringin Swift gives its characters one by one. - Skip spaces with
if c != " ". - Insert the letter. A letter already in the set changes nothing.
- Return
letters.count == 26. The comparison is alreadytrueorfalse, 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 returnstrue. - Counting characters instead of different ones.
sentence.countis the length of the text. Only the set'scountcounts 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,
Aandawould be two different characters in the set.
