Start learning free

LESSON 220 OF 289 · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

Count the Vowels in TypeScript: for...of and includes

Practice Read the full explanation

Given a string s, return how many vowels it has.

The vowels are a, e, i, o and u, in lower case or in upper case. The letter y is not a vowel here. Every other character (consonants, spaces, digits, punctuation) is skipped.

 

Example 1:

Input: s = "Hello World"
Output: 3
Explanation: The vowels are e, o and o. The other characters, the space included, are skipped.

Example 2:

Input: s = "AlgoCademy"
Output: 4
Explanation: A, o, a and e are vowels: the upper-case A counts too. The y doesn't count.

Example 3:

Input: s = "Why try?"
Output: 0
Explanation: y is not a vowel, so this string has none.

 

Constraints:

  • s has at least one character and doesn't start or end with a space.
  • s contains English letters, digits, spaces and punctuation.
STEP 1 OF 5
    Ctrl + Enter to check

    TYPESCRIPT · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

    Count The Vowels in TypeScript, explained

    Problem overview

    The method countVowels receives a string s and returns how many vowels it has. The vowels are a, e, i, o and u, in lower case or upper case. The letter y is not a vowel here, and every other character is skipped: consonants, spaces, digits and punctuation.

    In "Hello World" the vowels are e, o and o, so the answer is 3. In "AlgoCademy" they are A, o, a and e, which makes 4: the capital A counts and the y does not. "Why try?" has none, so the answer is 0.

    Approach

    Look at each character once and ask whether it is a vowel. The quick way to ask is to keep all ten vowels, both cases, in one string, and check whether the character appears in it. That avoids converting the input to lower case and avoids a long chain of comparisons.

    A counter starts at 0 and goes up by one for every character found among the vowels. After the last character the counter is the answer.

    Step by step

    1. Store the vowels in a constant string, vowels, holding "aeiouAEIOU". It never changes, so const fits. Both cases are in it and y is not.
    2. Declare count with let as a number, starting at 0. It changes in the loop.
    3. Visit every character with for (const c of s). A for...of loop over a string gives each character in order, as a one-character string.
    4. Test and count. vowels.includes(c) is true when c appears in vowels. If so, count++. Other characters are not in the string and are skipped.
    5. Return count after the loop, so that every character has been checked first.

    The solution in TypeScript

    class Solution {
        countVowels(s: string): number {
            const vowels: string = "aeiouAEIOU";
            let count: number = 0;
            for (const c of s) {
                if (vowels.includes(c)) {
                    count++;
                }
            }
            return count;
        }
    }
    
    • const c in a for...of is fine. Each turn of the loop creates a fresh c, so it is never reassigned.
    • includes on a string checks for a substring. "aeiouAEIOU".includes("e") is true. Because c is always a single character here, a hit means exactly one vowel. Passing a longer piece, such as "ei", would also give true, and so would the empty string, so includes only answers the question when the input is one character.
    • Case matters. "a" and "A" are different characters, which is why both are in the string.
    • Only the ten listed letters count. A string like "héllo" yields h, é, l, l, o, and é is not in vowels, so it would not be counted. The lesson's input is English text, so this only matters if you reuse the idea elsewhere.
    • Types are labels. The annotations on vowels, count and s are erased when the code compiles to JavaScript.

    Complexity

    Time: O(n), where n is the length of s. Each character is visited once, and includes scans a string of just ten characters, which is a constant amount of work.

    Space: O(1). The vowel string and the counter have a fixed size.

    Edge cases and common mistakes

    • No vowels. "Why try?" returns 0. The loop runs, finds nothing and returns the starting 0.
    • Upper-case vowels. Leaving them out of vowels would make "AlgoCademy" return 3 instead of 4.
    • The letter y. It is not in vowels, so it is skipped.
    • Spaces, digits and punctuation. They are not in vowels and don't count.
    • Returning inside the loop. A return count inside the if would stop after the first vowel and give 1 for "Hello World".
    • Using const for count. The compiler reports an error on count++, because a constant can't change.

    Testing

    • s = "Hello World" gives 3.
    • s = "AlgoCademy" gives 4.
    • s = "Why try?" gives 0.
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free