Start learning free

LESSON 241 OF 674 · SETS

Every Letter in C#: Checking a Sentence 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

    C# · SETS

    Every Letter in C#, explained

    Problem overview

    The method UsesEveryLetter gets a string sentence made of lowercase letters and spaces. It returns true if 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 "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 and the other 25 appear, so the answer is false. "abc abc abc" has only three different letters, so it is false too.

    Approach

    Collect the letters you meet in a HashSet<char>. A set keeps each value once, however many times it is added, so after one pass over the sentence it holds exactly the different letters. If the sentence uses only lowercase letters, there are 26 possible letters, and a set of 26 different ones must contain all of them.

    The only trap is the space. It is a character too, so it would go into the set as a 27th value. Skip spaces while collecting.

    Step by step

    1. Create an empty HashSet<char> named letters.
    2. Loop over the string with foreach (char c in sentence). A string is a sequence of char values, so each pass gives one character.
    3. Skip spaces with the test c != ' '. Chars use single quotes.
    4. Add c to letters inside the if. A repeat changes nothing.
    5. Return letters.Count == 26. The comparison is already a bool, so it can be returned as it is.

    The solution in C#

    class Solution {
        public bool UsesEveryLetter(string sentence) {
            HashSet<char> letters = new HashSet<char>();
            foreach (char c in sentence) {
                if (c != ' ') {
                    letters.Add(c);
                }
            }
            return letters.Count == 26;
        }
    }
    

    The details worth knowing:

    • A char literal uses single quotes (' '); a string literal uses double quotes (" "). Comparing a char with a string doesn't compile.
    • The lesson's tests print the result as lowercase true or false. Printing a bool with Console.WriteLine in your own program gives True or False.
    • The size is Count, as for every collection.

    Testing

    On the statement's three sentences, the method returns true, false and false. Another input worth trying is a short sentence such as "abc abc": it also returns false, because three different letters are far from 26. Try a sentence with all 26 letters and a few extra repeated words too; the repeats must not change the answer.

    Edge cases and common mistakes

    • Not skipping the space. For the first sentence the set would then hold 27 values (26 letters and the space), so Count == 26 would be false for a sentence that should pass.
    • A wrong answer in the other direction. Without the space check, a sentence with only 25 different letters and a space has 26 values in the set, and the method would wrongly return true. An example is "abcdefghijklmnopqrstuvwx y": the letters a to x and y, so 25 letters, plus the space.
    • Counting characters instead of different letters. A sentence has many more than 26 characters; only the set's size tells you how many different letters there are.
    • Checking >= 26. It works here, since the input has only lowercase letters and spaces, but == 26 says exactly what you mean.
    Keep going with C#500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free