Problem overview
The method usesEveryLetter receives a string sentence 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.
"my jumpy dog fixed a big quiz with seven lucky parrots" returns True: all 26 letters appear. "six jumpy kittens quiz the brave fox and a wild hog" returns False, because the letter c is missing. "abc abc abc" returns False: it has only three different letters.
Approach
The question is how many different letters the sentence holds, not how often each appears. That is what a set is for. Put the letters in a set and repeats vanish. Since the sentence has only lowercase letters (once spaces are left out), there are 26 possible values, and the set has all of them exactly when its size is 26.
Step by step
- Create an empty set
letters = set(). - Loop over the characters with
for c in sentence. Eachcis a one-character string. - Test
c != " ", because a space is not a letter and must stay out of the set. - Add
ctoletters. - Return
len(letters) == 26.
The solution in Python
class Solution:
def usesEveryLetter(self, sentence: str) -> bool:
letters = set()
for c in sentence:
if c != " ":
letters.add(c)
return len(letters) == 26
The comparison len(letters) == 26 is already True or False, so it is returned directly, without an if. Without the space check the set could hold up to 27 values, and a sentence missing a letter but containing a space could reach 26 and give a wrong True. The size test is valid because the input is guaranteed to hold only lowercase letters and spaces.
Another way
A string can be turned into a set directly. len(set(sentence.replace(" ", ""))) == 26 removes the spaces first and gives the same results on the three examples: True, False, False. The loop version is the one the lesson builds step by step.
Reading the second example
In "six jumpy kittens quiz the brave fox and a wild hog", the loop meets many repeated letters, such as i, t and a, and the set absorbs them. When the loop ends it holds 25 different letters: every letter except c. The test 25 == 26 is false, so the method returns False. In "abc abc abc" the set ends with just a, b and c, so the size is 3.
Edge cases
A sentence with many spaces and few letters, such as "a b c", simply has three letters in the set. A very short sentence returns False, since 26 different letters need at least 26 characters. The sentence is guaranteed to hold at least one letter and not to start or end with a space, so no special case is needed.
Testing
| sentence | result |
|---|---|
"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 |
Common mistakes
- Letting spaces into the set. A space counts as a value, so a sentence with 25 different letters and a space has 26 values and wrongly passes.
- Counting characters instead of different letters. A long sentence of repeated letters has many characters but few letters.
- Using a list for
letters. Thenlencounts repeats too, and the result is nearly always wrong. - Writing
if len(letters) == 26: return Trueinside the loop without a final return. A sentence that never reaches 26 returnsNone, notFalse.
