Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Scan the string, incrementing a counter whenever the current character equals `ch`.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you tally all matches without early exit.
Where candidates slip
Stopping at the first occurrence.
How to stand out
Mention the frequency-map preprocessing for many queries.
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
Count matches
1Hint 1Explore hintHide hint
Tally every match — should you stop early?
2Hint 2Explore hintHide hint
One counter, bump on equality, visit all characters.
3Hint 3Explore hintHide hint
Many targets? Build a frequency map once.
A · Core idea
Count how many times a target character appears: one pass, a counter bumped on each match, no early exit. It's count-occurrences for arrays applied to characters.
Why no early exit. You want the total, so unlike a search you must visit every character. Returning on the first match is the classic copy-from-search bug.
Comparing chars, not strings. Compare with == on char values (a value comparison) — fine. The Java == reference trap only bites when comparing String objects, not primitive chars.
Repeated queries. For many different target characters, a single frequency map answers each in O(1) after one O(n) build.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "banana", ch = 'a'b,n
no
count=03C · 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 — every character is compared once; no early termination since all matches count.
E · Edge cases and pitfalls5s = "hello", ch = 'z' — Absent → 0.Explore edge casesHide edge cases
Edge cases handled
s = "hello", ch = 'z'Absent → 0.s = "aaa", ch = 'a'All match → 3.s = "", ch = 'a'Empty → 0.
Pitfalls
- Returning after the first match.
- Comparing String objects with == when you meant char equality.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int count(String s, char ch) { int c = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ch) c++; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Returning after the first match.
- Comparing strings with == in Java instead of char equality (here we use char, so fine).
Revise
Carry the pattern into the interview
Pattern to remember
Count matches
Code pattern to memorize
c=0; for ch in s: if ch==target: c++Rules to remember
- 1Counter from 0.
- 2Bump on equality.
- 3Visit every char.
Use this pattern when
- Single-character tally
Interview takeaway
Character counting is a one-pass tally.
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: Count matches. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups