Start learning free

LESSON 242 OF 677 · SETS

Every Letter in Kotlin: Counting Distinct Characters

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

    KOTLIN · SETS

    Every Letter in Kotlin, 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 no space at the start or the end.

    The sentence "my jumpy dog fixed a big quiz with seven lucky parrots" contains 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 it is false. And "abc abc abc" has only three different letters, so it is false too.

    Approach

    You do not need to count how many times each letter appears; you only need to know which letters appear. That is what a set is for. Put the letters into a HashSet as you read the sentence. A repeat changes nothing, so each letter ends up in the set once. Then the set holds 26 letters exactly when all of a to z are present, because the sentence has no other characters than lowercase letters and spaces. The only thing to keep out is the space.

    Step by step

    1. Create an empty set of characters: hashSetOf<Char>(), in a val named letters.
    2. Loop over the string with for (c in sentence). A String can be looped over directly, and each c is a Char.
    3. Inside, check c != ' ', so spaces stay out. Note the single quotes: ' ' is a character, while " " would be a string and would not compare with a Char.
    4. Inside that if, add c to letters.
    5. After the loop, return letters.size == 26. The comparison is already true or false, so no if is needed.

    The solution in Kotlin

    class Solution {
        fun usesEveryLetter(sentence: String): Boolean {
            val letters = hashSetOf<Char>()
            for (c in sentence) {
                if (c != ' ') {
                    letters.add(c)
                }
            }
            return letters.size == 26
        }
    }
    

    The three examples give:

    true
    false
    false
    

    The tests print booleans in lowercase, as Kotlin's println does for a Boolean.

    Another way

    Kotlin can do the same in one line: sentence.filter { it != ' ' }.toSet().size == 26. The filter call keeps the characters that are not spaces, and toSet() removes the repeats. For "abc abc abc" the set has 3 letters, so the result is false, and for the first example it is true. The loop above is the same idea written out, and it is the form to learn first.

    More inputs to try

    The pangram "the quick brown fox jumps over the lazy dog" gives true. Change quick to uick and the letter q is gone, so the answer is false. The alphabet written in one word, "abcdefghijklmnopqrstuvwxyz", gives true: no spaces are needed, and each letter appears once.

    Complexity

    One step per character, so the time grows with the sentence's length. The set never holds more than 26 letters, so the extra space is constant.

    Common mistakes

    • Letting the space into the set. The first example has 26 letters and a space, so the set would hold 27 characters and the check == 26 would give false, which is wrong. Without the if, that is exactly what happens.
    • Counting characters instead of distinct ones. sentence.length says nothing about which letters appear. The set removes the repeats.
    • Using " " in the comparison. Kotlin rejects c != " " with the error operator '!=' cannot be applied to 'Char' and 'String'. Use the character literal ' '.
    Keep going with Kotlin500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free