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
- Create
letters := map[rune]bool{}. - Loop with
for _, c := range sentence. The first value, the byte position, goes into_. - If
c != ' ', setletters[c] = true. Adding a letter that is already there changes nothing. - Return
len(letters) == 26. The comparison is already abool, 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 whatcis 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.
