Problem overview
The function uses_every_letter 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" uses 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 the answer is false. In "abc abc abc" only three different letters appear.
Approach
Collect the different letters of the sentence in a HashSet<char>. A set keeps each letter once however often it shows up, so after the loop its size is the number of different letters. The sentence contains only lowercase English letters (besides spaces), and there are exactly 26 of those, so the sentence uses every letter exactly when the set holds 26.
Step by step
- Create an empty
HashSet<char>namedletters, declared withlet mut. - Loop over
sentence.chars(). A Rust string can't be indexed by number, butchars()gives its characters one by one, and eachcis a plainchar(not a reference). - Skip spaces with
if c != ' '. A space is not a letter, so it must stay out of the set. Otherwise the set could reach 26 with a space in it and only 25 letters. - Inside the
if,insertthe character. A repeat changes nothing. - After the loop, return
letters.len() == 26. The comparison is alreadytrueorfalse, so it is returned as it is.
The solution in Rust
impl Solution {
fn uses_every_letter(sentence: String) -> bool {
let mut letters: HashSet<char> = HashSet::new();
for c in sentence.chars() {
if c != ' ' {
letters.insert(c);
}
}
return letters.len() == 26;
}
}
A space is written ' ' with single quotes: that is a char, which is what c is. A string such as " " would be a different type and the comparison would not compile.
Trying it
"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 quick brown fox jumps over the lazy dog" -> true
For the second sentence, the set ends with 25 letters and does not contain 'c'.
Why the size of the set answers the question
After the loop, the set holds exactly the different letters that appeared. If one of the 26 letters never appeared, the set is missing it, and its size is at most 25. If all 26 appeared, the size is 26 and it can't be more, because the sentence has no other characters than letters and spaces. That is why a single length check is enough, and no loop over 'a'..='z' is needed.
Checking for one particular letter works too: letters.contains(&'c') asks whether a given letter is in. It takes a reference, like every contains. The size check is shorter because it covers all 26 at once.
Common mistakes
- Letting spaces into the set. Without the
c != ' 'check,"abc abc"would give a set of 4 characters (three letters and the space) rather than 3. - Counting characters instead of different letters. A plain counter, or the length of the sentence, grows with repeats. Only the set's size answers "how many different?".
- Comparing with a larger number. The answer needs exactly 26 different letters. Using a wrong limit, such as 27 to allow for the space, breaks the answer.
- Indexing the string.
sentence[0]does not compile in Rust; usechars().
