Start learning free

LESSON 245 OF 679 · SETS

Every Letter in Rust: Counting Letters With a HashSet

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

    RUST · SETS

    Every Letter in Rust, explained

    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

    1. Create an empty HashSet<char> named letters, declared with let mut.
    2. Loop over sentence.chars(). A Rust string can't be indexed by number, but chars() gives its characters one by one, and each c is a plain char (not a reference).
    3. 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.
    4. Inside the if, insert the character. A repeat changes nothing.
    5. After the loop, return letters.len() == 26. The comparison is already true or false, 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; use chars().
    Keep going with Rust500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free