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
- Create an empty
HashSet<char>namedletters. - Loop over the string with
foreach (char c in sentence). Astringis a sequence ofcharvalues, so each pass gives one character. - Skip spaces with the test
c != ' '. Chars use single quotes. - Add
ctolettersinside theif. A repeat changes nothing. - Return
letters.Count == 26. The comparison is already abool, 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
charliteral uses single quotes (' '); a string literal uses double quotes (" "). Comparing acharwith a string doesn't compile. - The lesson's tests print the result as lowercase
trueorfalse. Printing aboolwithConsole.WriteLinein your own program givesTrueorFalse. - 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 == 26would 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== 26says exactly what you mean.
