Problem overview
The function usesEveryLetter gets a string sentence made of lowercase letters and spaces. It returns true when 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 has 1 to 1,000 characters, at least one letter, and doesn't start or end with a space.
The sentence "my jumpy dog fixed a big quiz with seven lucky parrots" contains all 26 letters, so the answer is true. "six jumpy kittens quiz the brave fox and a wild hog" has 25 of them and no c, so the answer is false. "abc abc abc" has only three different letters, so the answer is false. The tests print the booleans as true and false.
Approach
The question is about which different letters appear, not how often. That is exactly what a set stores. Put every letter of the sentence in a set; repeats do nothing. Because the sentence has only lowercase English letters, there are exactly 26 possible different letters, so the set holds 26 values only if no letter is missing.
The first example shows the idea. Many of its letters are repeats, and the set ends up with exactly 26 different ones. In the third example, nine letters go in, but only a, b and c are different, so the set stays at 3.
Step by step
- Create an empty
unordered_set<char>namedletters. - Loop over the characters of
sentencewith acharnamedc. - Skip spaces with
if (c != ' '). A space is not a letter and must stay out of the set. - Insert the letter. If it is already in the set, nothing changes.
- Return the comparison
letters.size() == 26. It is alreadytrueorfalse, so noifis needed.
The solution in C++
class Solution {
public:
bool usesEveryLetter(string sentence) {
unordered_set<char> letters;
for (char c : sentence) {
if (c != ' ') {
letters.insert(c);
}
}
return letters.size() == 26;
}
};
Notes on the C++:
- Looping over a
stringwith a range-basedforgives its characters one by one, ascharvalues. - Character literals use single quotes (
' '); double quotes make a string. - A
boolis printed bycoutas 1 or 0 unless you ask for words withboolalpha. The function returns abool; how it is shown is up to the code that prints it.
Complexity
Time is O(n) for a sentence of n characters, with one set insert per letter. Space is O(1): the set never holds more than 26 letters, whatever the length of the sentence.
Edge cases and common mistakes
- Letting spaces into the set. If the check is left out, a sentence that has all 26 letters and also spaces ends with 27 values, and the function returns
falsefor the first example. - Counting characters instead of different letters. A sentence can be long and still miss letters, like
"abc abc abc". - Comparing with 26 when uppercase letters could appear. They can't here, because the sentence has only lowercase letters. If they could,
Aandawould be two values. - Short sentences. Anything under 26 letters is
falsewithout any special case.
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.
