Grokking the Coding Interview: Patterns for Coding Questions
0% completed
Introduction to Hash Map Pattern
You are given a string. Find the first character that appears exactly once.
"swiss" answer: "w"
"aabbcc" no unique character
A simple solution checks each character separately. For every position, scan the full string and count that character.
This repeats the same counting work many times. Its time complexity is O(N²).
A character's total count does not change while we check different positions. We should calculate each count only once.
Use two passes:
- Count every character with a hash map.
- Read the string again and return the first character with count
1.
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%