Tutorialspoint
Problem
Solution
Submissions

Number of Vowels

Certification: Basic Level Accuracy: 77.78% Submissions: 9 Points: 5

Write a Python program that counts the number of vowels (a, e, i, o, u) in a given string.

Example 1
  • Input: s = "Hello World"
  • Output: 3
  • Explanation:
    • Step 1: Initialize a counter variable to 0.
    • Step 2: Iterate through each character in the string "Hello World".
    • Step 3: Check if each character is a vowel (a, e, i, o, u) in either lowercase or uppercase.
    • Step 4: Vowels found: 'e' in "Hello", 'o' in "Hello", and 'o' in "World".
    • Step 5: Increment counter for each vowel found (total: 3).
    • Step 6: Return the final count of vowels: 3.
Example 2
  • Input: s = "C sharp"
  • Output: 1
  • Explanation:
    • Step 1: Initialize a counter variable to 0.
    • Step 2: Iterate through each character in the string "C sharp".
    • Step 3: Check if each character is a vowel (a, e, i, o, u) in either lowercase or uppercase.
    • Step 4: Only one vowel found: 'a' in "sharp".
    • Step 5: Increment counter once (total: 1).
    • Step 6: Return the final count of vowels: 1.
Constraints
  • 1 ≤ len(string) ≤ 10^6
  • The string may contain uppercase and lowercase letters.
  • The string contains printable ASCII characters.
  • Time Complexity: O(n), where n is the length of the string.
  • Space Complexity: O(1), it is using a counter.
StringsAmazonShopifyAdobe
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

The following are the steps to count the number of vowels in a string:


  • Iterate with a loop: Use a loop to check each character and count vowels.
  • Use list comprehension: sum(1 for char in s if char.lower() in "aeiou").
  • Use regular expressions: len(re.findall(r'[aeiouAEIOU]', s)).
  • Convert to lowercase: Convert the string to lowercase for easier comparison.

Submitted Code :