Grokking the Coding Interview: Patterns for Coding Questions
Vote

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:

  1. Count every character with a hash map.
  2. Read the string again and return the first character with count 1.

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content