Given a string s made of English letters, count how many of its characters are vowels. Both lowercase and uppercase a, e, i, o, u count.
Count Vowels in a String
SolvedExamples:
Input: s = "programming"
Output: 3
Explanation: The vowels are o, a and i.
Input: s = "Rhythm"
Output: 0
Explanation: There is no vowel in the word.
Constraints:
1 ≤ s.length ≤ 10^5s contains only English letters
Expected Complexities
Time Complexity: O(n) Auxiliary Space: O(1)
Company Tags
Topic Tags
Go through the string once and increase a counter whenever the current character, lowered to lowercase, is one of a, e, i, o, u. Putting the vowels in a small set or string makes the membership test a one-liner.
class Solution:
def countVowels(self, s):
return sum(1 for ch in s.lower() if ch in 'aeiou')
Sign in to see your submissions.