BudiBadu Logo

Vowel Counter

String Easy 5 views
Like30

Ever wondered how many vowels are hiding in a specific word or sentence? That’s what Vowel Counter is all about! You’re given a string, and your task is to count how many times the standard vowels (a, e, i, o, and u) show up, regardless of being uppercase or lowercase.

The "secret sauce" here is a simple Membership Check. As you walk through the string character by character, you ask the question: "Is this letter in my vowel set?" Think of it like a filter: every time you hit a match, you increment your counter and keep moving. It’s a fast, linear O(n) process that’s essential for text analysis, speech processing, and even simple word puzzles.

This challenge is a great way to practice string iteration and character comparison. It’s a basic building block for any text-processing task where you need to identify and count specific types of characters!

For text and token tasks, be precise with index movement and substring boundaries. Most hidden failures come from partial-match handling and boundary cuts, so keep comparisons explicit and avoid assumptions about implicit separators or formatting not guaranteed by the input contract.

Examples

Example 1
Input
s = "apple"
Output
2
Explanation

Example 1: "apple" contains 2 vowels (a, e)

Example 2
Input
s = "sky"
Output
0
Explanation

Example 2: "sky" contains no vowels

Example 3
Input
s = "education"
Output
5
Explanation

Example 3: "education" contains 5 vowels (e, u, a, i, o)

Algorithm Flow

Recommendation Algorithm Flow for Vowel Counter - Budibadu
Recommendation Algorithm Flow for Vowel Counter - Budibadu

Best Answers

java
class Solution {
    public int count_vowels(String s) {
        int count = 0;
        String vowels = "aeiouAEIOU";
        for (char c : s.toCharArray()) {
            if (vowels.indexOf(c) != -1) count++;
        }
        return count;
    }
}