03
Hashing & Frequency Maps
O(1) lookups and counting.
5 pages
- Pattern 03: Hashing / Frequency MapsPattern guide
Three skeletons cover nearly every hashing problem: count, seen-before, and prefix-sum + map.
- LC 242Valid Anagram (LC 242)Easy
Two strings are anagrams if every character appears the same number of times in each.
- LC 217Contains Duplicate (LC 217)Easy
A duplicate exists if we ever see a value we have already stored in a set (or if any frequency exceeds one).
- LC 383Ransom Note (LC 383)Easy
You can build the note only if every character’s demand in ransomNote is covered by the supply in magazine, with each magazine letter used at most once.
- LC 560Subarray Sum Equals K (LC 560)Medium
Store prefix sum frequencies to find subarrays with target sum in O(n)