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 at least once, and false otherwise. Spaces are not letters, so they don't count, and a letter may appear many times.
The sentence "my jumpy dog fixed a big quiz with seven lucky parrots" has 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. "abc abc abc" has only three different letters, so it is false too.
Approach
The question is how many different letters the sentence has. A Set counts different values for free: adding a letter that is already in it changes nothing. So collect every letter in a set, leave the spaces out, and look at the size. The sentence has only lowercase letters, and there are 26 of them, so a set of 26 different letters means none is missing.
Step by step
- Create
letters, an empty set, withnew Set(). - Loop over the characters with
for (let c of sentence). A string is iterable, so eachcis one character: a letter or a space. - Skip spaces:
if (c != " "), because a space is not a letter and must stay out of the set. - Add
ctoletters. - Return
letters.size == 26. The comparison is alreadytrueorfalse, so it is returned as it is.
The solution in JavaScript
class Solution {
usesEveryLetter(sentence) {
let letters = new Set();
for (let c of sentence) {
if (c != " ") {
letters.add(c);
}
}
return letters.size == 26;
}
}
Two JavaScript details help here. Looping over a string with for...of gives one character at a time, so c is a one-character string. And the set's count is size, not length.
Another way
You can take the spaces out first and build the set straight from the string, because a set can be built from anything you can loop over:
function usesEveryLetterShort(sentence) {
return new Set(sentence.replaceAll(" ", "")).size === 26;
}
On the three examples it returns true, false and false, like the lesson's version.
Testing
On the statement's examples, the lesson's solution prints:
true
false
false
The tests print booleans in lowercase.
For "abc abc abc" the loop adds a, b and c, skips every space, and ends with the set [ 'a', 'b', 'c' ]. Its size is 3, which is not 26, so the answer is false. For the first sentence the set ends with 26 different letters, so the answer is true. Without the space check, that sentence would give a set of 27 values.
Common mistakes
- Letting the space into the set. The set then holds 27 values for the first example, and the check
== 26isfalsefor a sentence that has every letter. For "abc abc", the set holds 4 values (a, b, c and the space), not 3. - Using
length. A set has nolength, soletters.length == 26comparesundefinedwith 26 and is alwaysfalse. - Checking
letters.size > 25or counting the sentence's characters. The number of characters says nothing about how many are different. Only the set's size does. - Returning inside the loop. Do the check after the loop, once every character has been added.
