Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
For each letter, normalise case and test if it's one of a,e,i,o,u; increment vowels or consonants accordingly.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you use set membership and exclude non-letters.
Where candidates slip
Treating any non-vowel (including spaces/digits) as a consonant.
How to stand out
Note the design lets you add 'y' or other alphabets trivially.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Vowel-set membership
1Hint 1Explore hintHide hint
How do you test if a character is one of a,e,i,o,u cleanly?
2Hint 2Explore hintHide hint
Lower-case first so 'A' and 'a' share one check.
3Hint 3Explore hintHide hint
Only count consonants for characters that are actually letters.
A · Core idea
Classify each letter by set membership in the fixed vowel set {a,e,i,o,u}: a vowel if it's in the set, otherwise (if it's a letter) a consonant.
Why normalise case first. Lower-casing each character means one membership test handles both 'A' and 'a'. Without it you'd need to check ten characters instead of five.
Why guard with isLetter. Spaces, digits, and punctuation are neither vowels nor consonants. Counting them as consonants (the default 'else') is the common bug — gate the consonant branch on 'is this actually a letter?'.
Why a set, not a chain of ORs. Membership in a small fixed alphabet is clearest as a set lookup; it also generalises if the alphabet grows (e.g. treating 'y' as a vowel).
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "Hi!"H
letter, not vowel
cons=1[1, 1]C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(1) space — one case-fold and one membership test per character.
E · Edge cases and pitfalls5s = "xyz" — No vowels → [0, 3].Explore edge casesHide edge cases
Edge cases handled
s = "xyz"No vowels → [0, 3].s = "aeiou"All vowels → [5, 0].s = "a1 b"Digit and space skipped → vowels 1, consonants 1.
Pitfalls
- Counting uppercase vowels as consonants (normalise case).
- Counting spaces/digits as consonants.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int[] countVowelsConsonants(String s) { int vowels = 0, consonants = 0; String set = "aeiou"; for (char ch : s.toLowerCase().toCharArray()) { if (Character.isLetter(ch)) {Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Counting uppercase vowels as consonants (normalise case first).
- Counting spaces or digits as consonants.
Revise
Carry the pattern into the interview
Pattern to remember
Set membership classify
Code pattern to memorize
if ch in 'aeiou' vow++ elif isLetter(ch) cons++Rules to remember
- 1Lower-case first.
- 2Vowel ⟺ in {a,e,i,o,u}.
- 3Other letters → consonant.
Use this pattern when
- Classifying over a small fixed alphabet
Interview takeaway
Fixed small alphabets are best tested with set membership.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Set membership classify. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups