Problem overview
The function usesEveryLetter gets a string sentence made of lowercase letters and spaces. It returns true when every letter from a to z appears in it at least once, and false otherwise. Spaces are not letters, and a letter may appear many times. The sentence has 1 to 1,000 characters, at least one letter, and no space at the start or the end.
The sentence "my jumpy dog fixed a big quiz with seven lucky parrots" contains all 26 letters, so the answer is true. In "six jumpy kittens quiz the brave fox and a wild hog" the letter c is missing, so it is false. And "abc abc abc" has only three different letters, so it is false too.
Approach
You do not need to count how many times each letter appears; you only need to know which letters appear. That is what a set is for. Put the letters into a HashSet as you read the sentence. A repeat changes nothing, so each letter ends up in the set once. Then the set holds 26 letters exactly when all of a to z are present, because the sentence has no other characters than lowercase letters and spaces. The only thing to keep out is the space.
Step by step
- Create an empty set of characters:
hashSetOf<Char>(), in avalnamedletters. - Loop over the string with
for (c in sentence). AStringcan be looped over directly, and eachcis aChar. - Inside, check
c != ' ', so spaces stay out. Note the single quotes:' 'is a character, while" "would be a string and would not compare with aChar. - Inside that
if, addctoletters. - After the loop, return
letters.size == 26. The comparison is alreadytrueorfalse, so noifis needed.
The solution in Kotlin
class Solution {
fun usesEveryLetter(sentence: String): Boolean {
val letters = hashSetOf<Char>()
for (c in sentence) {
if (c != ' ') {
letters.add(c)
}
}
return letters.size == 26
}
}
The three examples give:
true
false
false
The tests print booleans in lowercase, as Kotlin's println does for a Boolean.
Another way
Kotlin can do the same in one line: sentence.filter { it != ' ' }.toSet().size == 26. The filter call keeps the characters that are not spaces, and toSet() removes the repeats. For "abc abc abc" the set has 3 letters, so the result is false, and for the first example it is true. The loop above is the same idea written out, and it is the form to learn first.
More inputs to try
The pangram "the quick brown fox jumps over the lazy dog" gives true. Change quick to uick and the letter q is gone, so the answer is false. The alphabet written in one word, "abcdefghijklmnopqrstuvwxyz", gives true: no spaces are needed, and each letter appears once.
Complexity
One step per character, so the time grows with the sentence's length. The set never holds more than 26 letters, so the extra space is constant.
Common mistakes
- Letting the space into the set. The first example has 26 letters and a space, so the set would hold 27 characters and the check
== 26would givefalse, which is wrong. Without theif, that is exactly what happens. - Counting characters instead of distinct ones.
sentence.lengthsays nothing about which letters appear. The set removes the repeats. - Using
" "in the comparison. Kotlin rejectsc != " "with the erroroperator '!=' cannot be applied to 'Char' and 'String'. Use the character literal' '.
