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
- Store the vowels in a constant string,
vowels, holding"aeiouAEIOU". It never changes, soconstfits. Both cases are in it andyis not. - Declare
countwithletas anumber, starting at 0. It changes in the loop. - Visit every character with
for (const c of s). Afor...ofloop over a string gives each character in order, as a one-character string. - Test and count.
vowels.includes(c)istruewhencappears invowels. If so,count++. Other characters are not in the string and are skipped. - Return
countafter 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 cin afor...ofis fine. Each turn of the loop creates a freshc, so it is never reassigned.includeson a string checks for a substring."aeiouAEIOU".includes("e")istrue. Becausecis always a single character here, a hit means exactly one vowel. Passing a longer piece, such as"ei", would also givetrue, and so would the empty string, soincludesonly 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"yieldsh,é,l,l,o, andéis not invowels, 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,countandsare 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
vowelswould 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
vowelsand don't count. - Returning inside the loop. A
return countinside theifwould stop after the first vowel and give 1 for"Hello World". - Using
constforcount. The compiler reports an error oncount++, because a constant can't change.
Testing
s = "Hello World"gives3.s = "AlgoCademy"gives4.s = "Why try?"gives0.
