Vowels and Consonants
Character Set CheckSolved Oct 30, 2025
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven a string S, count and report the total number of vowels and the total number of consonants.ReadHide
Problem Description
Given a string S, count and report the total number of vowels and the total number of consonants.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Use a predefined set (HashSet in Java/C++, set in Python) of vowels for fast O(1) lookups during a single string traversal.
Approaches
1. Hash Set Lookup (Optimal) - O(N) ⭐
Idea: Create a small, constant-size Hash Set containing all vowel characters. Iterate through the string once, checking if each character is present in the set (O(1) lookup time).
Time: O(N)
Space: O(1) (Set size is constant).
Solution code
import sys
def solve_vowels_consonants():
try:
S = sys.stdin.readline().strip().lower()
except:
return
# Optimized: O(N) Time, O(1) Space - Using a Set for O(1) lookup
VOWELS = {'a', 'e', 'i', 'o', 'u'}
vowel_count = 0
consonant_count = 0
for char in S:
if 'a' <= char <= 'z':
if char in VOWELS:
vowel_count += 1
else:
consonant_count += 1
print(f"Vowels: {vowel_count}")
print(f"Consonants: {consonant_count}")
# Brute Force: O(N) Time - Using a long 'if-elif' chain instead of a set (less clean, same complexity)
def solve_vowels_consonants_brute():
solve_vowels_consonants()
if __name__ == "__main__":
solve_vowels_consonants()Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language