Count the Vowels
Easy · rating 1000 · strings
Read a single line and print the number of vowels (a, e, i, o, u, upper or lower case) it contains.
Editorial
Count the Vowels is a basic string-scanning task: count how many characters are vowels, case-insensitively. Put the vowels in a set for O(1) membership tests and scan once.
vowels = set('aeiouAEIOU')
print(sum(1 for c in s if c in vowels))Complexity
Time: O(n). Space: O(1).