Start learning free

LESSON 237 OF 667 · SETS

Every Letter in C++: Checking for a Pangram with a Set

Practice Read the full explanation

You get a string sentence made of lowercase letters and spaces.

Return true if every letter from a to z appears in it at least once. Otherwise, return false.

Spaces are not letters, so they don't count. A letter may appear many times.

 

Example 1:

Input: sentence = "my jumpy dog fixed a big quiz with seven lucky parrots"
Output: true
Explanation: All 26 letters appear at least once.

Example 2:

Input: sentence = "six jumpy kittens quiz the brave fox and a wild hog"
Output: false
Explanation: The letter c is missing.
The other 25 letters all appear.

Example 3:

Input: sentence = "abc abc abc"
Output: false
Explanation: Only 3 different letters appear: a, b and c.

 

Constraints:

  • sentence has 1 to 1,000 characters.
  • It has only lowercase English letters and spaces.
  • It has at least one letter and doesn't start or end with a space.
STEP 1 OF 5
    Ctrl + Enter to check

    C++ · SETS

    Every Letter in C++, explained

    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

    1. Create an empty unordered_set<char> named letters.
    2. Loop over the characters of sentence with a char named c.
    3. Skip spaces with if (c != ' '). A space is not a letter and must stay out of the set.
    4. Insert the letter. If it is already in the set, nothing changes.
    5. Return the comparison letters.size() == 26. It is already true or false, so no if is 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 string with a range-based for gives its characters one by one, as char values.
    • Character literals use single quotes (' '); double quotes make a string.
    • A bool is printed by cout as 1 or 0 unless you ask for words with boolalpha. The function returns a bool; 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 false for 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, A and a would be two values.
    • Short sentences. Anything under 26 letters is false without any special case.

    Testing

    • "my jumpy dog fixed a big quiz with seven lucky parrots" gives true.
    • "six jumpy kittens quiz the brave fox and a wild hog" gives false.
    • "abc abc abc" gives false.
    Keep going with C++500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free