This is an open source platform.
Create coding videos, upload them on YouTube, and help the community grow.
Teaching is the fastest way to learn β and the best way to never forget
what you know π
β‘ Start Recording
Screen only
Screen With Camera
β‘Start Recording
β
Selected Template
${temp1}
Screen only
Screen With Camera
Start Recording
β
β‘ Show Preview
Thumbnail Templates
Generating Thumbnail...
Master In-Demand Tech. Get Paid
Count Vowels in a String
Given a string s, count the number of vowels (a, e, i, o, u) present in the string.
Difficulty:
JavaScript Solution
function countVowels(s) {
let count = 0;
for (let ch of s.toLowerCase()) {
if ('aeiou'.includes(ch)) count++;
}
return count;
}
Explanation: We iterate through each character and increment the count if it is a vowel.
β± O(n) | πΎ O(1)
Java Solution
class Solution {
public int countVowels(String s) {
int count = 0;
for (char c : s.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) != -1) count++;
}
return count;
}
}
Explanation: Each character is checked against a vowel set.
β± O(n) | πΎ O(1)
Python Solution
def count_vowels(s):
return sum(1 for c in s.lower() if c in 'aeiou')
Explanation: We count characters that belong to the vowel set.