Problem overview
The method usesEveryLetter receives a string sentence made of lowercase English letters and spaces. It returns true when every letter from a to z appears at least once, and false otherwise. Spaces are not letters and do not count, and a letter may appear many times. The sentence has 1 to 1,000 characters and neither starts nor ends with a space.
For "my jumpy dog fixed a big quiz with seven lucky parrots" all 26 letters appear, so the answer is true. For "six jumpy kittens quiz the brave fox and a wild hog" the letter c is missing, so the answer is false. For "abc abc abc" only three different letters appear, so the answer is false.
Approach: count the different letters
The question is not how many letters there are, but how many different ones. A Set counts different values by itself: it keeps each value once, however many times you add it. Put the sentence's letters in a set. If the set ends up with 26 entries, every letter is there.
That works because the sentence contains only lowercase letters and spaces. Once the spaces are left out, the set can hold at most 26 different values, and 26 different values must be exactly the 26 letters.
Step by step
- Create
letters, an emptySet<string>. A character of a string is itself a string of length 1, so the set holds strings. - Loop over the characters with
for (let c of sentence). A string can be looped over directly withfor...of. - Skip the spaces with
if (c != " "), so they stay out of the set. - Add
ctoletters. A letter already there changes nothing. - Return
letters.size == 26. The comparison is alreadytrueorfalse, so it is returned as it is, without anif.
The solution in TypeScript
class Solution {
usesEveryLetter(sentence: string): boolean {
let letters: Set<string> = new Set();
for (let c of sentence) {
if (c != " ") {
letters.add(c);
}
}
return letters.size == 26;
}
}
Some details:
- The space must stay out.
new Set("abc abc abc")has 4 entries:a,b,cand the space. The loop skips the space, so the set has 3. A sentence with 25 letters and a space would otherwise reach a size of 26 and wrongly returntrue. - Looping over a string with
for...ofvisits it one character at a time, and each character is a string. - Returning a comparison.
letters.size == 26is a boolean. The lesson's tests printtrueandfalsein lowercase.
Another way
You can remove the spaces first and build the set from what is left:
function usesEveryLetter(sentence: string): boolean {
return new Set(sentence.replaceAll(" ", "")).size === 26;
}
It gives true, false and false on the three examples. replaceAll returns a new string without spaces, and new Set accepts any iterable, a string included. The lesson's version spells out the loop so that each step is visible.
Edge cases and common mistakes
- Counting spaces. Without the
c != " "test, a sentence that misses one letter but contains a space reaches size 26 and returnstrueby mistake. - Checking the sentence length. The length says nothing about the different letters:
"abc abc abc"is 11 characters long and has only 3 different letters. - Exactly the alphabet.
"abcdefghijklmnopqrstuvwxyz"returnstrue: the sentence may be only the 26 letters, with no space at all.
Testing
"my jumpy dog fixed a big quiz with seven lucky parrots"givestrue."six jumpy kittens quiz the brave fox and a wild hog"givesfalse."abc abc abc"givesfalse."abcdefghijklmnopqrstuvwxyz"givestrue.
