Start learning free

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

Count the Vowels in Kotlin: Looping Over a String

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

    KOTLIN · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

    Count The Vowels in Kotlin, explained

    Problem overview

    The function countVowels receives a string s and returns how many vowels it has. The vowels are a, e, i, o and u, in lower or upper case. The letter y does not count. Everything else, such as consonants, spaces, digits and punctuation, is skipped.

    In "Hello World" the vowels are e, o and o, so the answer is 3. In "AlgoCademy" they are A, o, a and e, so the answer is 4: the upper-case A counts and the y doesn't. In "Why try?" there are none, so the answer is 0.

    Approach

    Visit every character once, test whether it is a vowel, and count the ones that are. The test is a lookup in a string that holds all ten vowel characters, upper and lower case. Listing both cases avoids converting each character first.

    Going through "Hello World" character by character, only three of the eleven characters pass the test. The counter reaches 1 at the e, 2 at the first o and 3 at the second o. The H, the l characters, the space, the W, the r and the d fail the test and leave the counter alone.

    The test is the only place where the exercise's rules live. If the rules changed, for example to count y, you would change the vowels string and nothing else.

    Step by step

    1. Store the vowels in a val named vowels: "aeiouAEIOU". The value never changes, so val is right.
    2. Create count with var, starting at 0. It changes inside the loop.
    3. Loop over the string with for (c in s). Each turn gives one character as a Char.
    4. Test the character. c in vowels is true when c appears anywhere in the string vowels. If so, do count++.
    5. Return count after the loop, so every character has been checked first.

    The solution in Kotlin

    class Solution {
        fun countVowels(s: String): Int {
            val vowels = "aeiouAEIOU"
            var count = 0
            for (c in s) {
                if (c in vowels) {
                    count++
                }
            }
            return count
        }
    }
    

    Some details worth knowing:

    • A String is a sequence of Char. The for loop hands out characters one at a time, in order. A Char is written with single quotes, such as 'e', and a String with double quotes.
    • in works on strings. 'e' in "aeiou" is true. The check is case-sensitive: 'E' in "aeiou" is false, which is why the lesson's string lists both cases.
    • The loop variable is a val. c holds a new character on each turn and can't be reassigned inside the loop.
    • A shorter form exists. s.count { it in vowels } counts the characters that pass a test and gives 3 on "Hello World". The explicit loop shows what that call does.

    Complexity

    Time is O(n) for a string of length n, since the lookup in the ten-character vowels string takes a constant number of comparisons. Space is O(1).

    Edge cases and common mistakes

    • Upper-case vowels. Leaving them out of vowels gives 3 instead of 4 for "AlgoCademy".
    • The letter y. It isn't in the string, so "Why try?" gives 0.
    • Returning inside the loop. The function would stop after the first character.
    • Counting once per distinct vowel. Every occurrence counts: "Hello World" has two o's and the answer includes both.

    Testing

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