Problem overview
The function countVowels takes a string s and returns how many vowels it contains. The vowels are a, e, i, o and u, in lower or upper case. The letter y doesn't count. Consonants, spaces, digits and punctuation are 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 capital A counts and the y doesn't. In "Why try?" there are none, so the answer is 0.
Approach
Look at each character once, ask "is this a vowel?", and keep a counter. The question is the interesting part. Instead of comparing against ten letters with a long chain of conditions, store all the vowels in one string, "aeiouAEIOU", and look the character up in it. Both cases are in the string, so no case conversion is needed.
Strings have an indexOf method that returns the position of the first match, or -1 when there is none. It accepts a single Char, so vowels.indexOf(c) is -1 exactly when c is not a vowel. For example, "aeiouAEIOU".indexOf('e') is 1, indexOf('A') is 5, and indexOf('y') is -1.
Step by step
- Store the ten vowels in
val vowels. It is avalbecause the set of vowels never changes. - Create
var count, starting at 0. It changes inside the loop, so it must be avar. - Write a
forloop with a variablecovers. Looping over aStringgives each character as aChar. - Inside the loop, if
vowels.indexOf(c)is not -1, add 1 tocountwithcount += 1. - After the loop, return
count. Returning inside the loop would stop after the first character.
For "Hello World", the characters that pass the check, and the count after each, are:
| Character | count after it |
|---|---|
| e | 1 |
| o | 2 |
| o | 3 |
The other characters, H, l, l, the space, W, r, l and d, are not in vowels and change nothing.
The solution in Scala
class Solution {
def countVowels(s: String): Int = {
val vowels = "aeiouAEIOU"
var count = 0
for (c <- s) {
if (vowels.indexOf(c) != -1) {
count += 1
}
}
return count
}
}
Details that matter in Scala:
for (c <- s)reads "for eachcins". The<-arrow is the generator. A string behaves like a sequence of characters, andcis aChar.!=compares values. Here it compares twoIntnumbers.- A character literal uses single quotes, like
'e'. A string of one character uses double quotes. The two are different types, andindexOfis called with aCharin this solution becausecis one. - Scala has no
++for numbers, so usecount += 1.
Edge cases and common mistakes
- Upper-case vowels. With only
"aeiou"invowels, the A of"AlgoCademy"would be missed and the answer would be 3 instead of 4. - The letter y. It isn't in
vowels, so"Why try?"gives 0. - Checking against -1.
indexOfreturns 0 for the first character ofvowels, which is a perfectly good match. A test such asindexOf(c) > 0would skip every lower-casea. - Counting once per character. Each character of
sis checked once, so repeated vowels count each time they appear.
Testing
countVowels("Hello World") -> 3
countVowels("AlgoCademy") -> 4
countVowels("Why try?") -> 0
