Problem overview
The function countVowels receives 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 is not a vowel here, and every other character (consonants, spaces, digits, punctuation) is skipped.
"Hello World" has 3 vowels: e, o and o. "AlgoCademy" has 4: A, o, a and e, and the upper-case A counts. "Why try?" has 0, because y doesn't count.
Approach
Look at every character once, and add 1 to a counter when the character is one of the ten vowel characters. Two Go tools make this short:
for _, c := range svisits each character of a string.strings.ContainsRune(vowels, c)reports whether the charactercappears anywhere in the stringvowels.
Putting both cases into one string, "aeiouAEIOU", avoids converting s to lower case first.
Step by step
- Store the vowels in a variable named
vowels, declared with:=:"aeiouAEIOU". There are ten characters, five lower and five upper, and no y. - Create
countwith:=, starting at 0. No vowel has been found yet. - Loop over the string with
range, using_for the index andcfor the character. Write the closing brace right away. - Test the character. If
strings.ContainsRune(vowels, c)is true, add 1 tocountwith++. Other characters are not invowels, so they are skipped. - Return
countafter the loop. Returning inside the loop would stop at the first character.
The solution in Go
func countVowels(s string) int {
vowels := "aeiouAEIOU"
count := 0
for _, c := range s {
if strings.ContainsRune(vowels, c) {
count++
}
}
return count
}
What to know about these Go details:
rangeover a string gives runes. Eachcis arune, which is an integer holding a Unicode code point, and not a one-character string. That is the typeContainsRunetakes. A vowel such as'e'is written with single quotes when you need a rune literal.- The index comes first.
rangeyields an index and a value. Go refuses unused variables, so the index is discarded with_. - Indexes count bytes. For
"héy", ranging gives the indexes 0, 1 and 3, becauseétakes two bytes in UTF-8, and the character values 104, 233 and 121. This is whyrangeis better thans[i], which gives one byte at a time, when text can contain non-ASCII characters. - The condition has no parentheses. Go writes
ifconditions without them, and the braces are required.
Complexity
The loop visits each character once, and each ContainsRune call scans a string of ten characters, a fixed amount of work, so the time is proportional to the length of s. The space is constant.
Edge cases and common mistakes
- Upper-case vowels. Leaving out
AEIOUmakes"AlgoCademy"count 3 instead of 4. - The letter y.
"Why try?"must return 0. - Non-letters. Spaces, digits and punctuation are not in
vowels, so they never count. - Returning too early. A
returninside the loop ends the function at the first character. - Using a string for
c. Becausecis a rune,strings.Contains(vowels, c)doesn't compile. UseContainsRune, as the lesson does.
Testing
"Hello World"gives 3."AlgoCademy"gives 4."Why try?"gives 0."a1e!"gives 2.
