Hash-set pattern [mp]
Micro pattern¶
Description¶
Store unique values or states when membership rather than an associated value is needed.
Problems¶
Detect duplicates or revisited states¶
Check membership to detect repeated inputs, visits, or generated states.
- 217. Contains Duplicate (Top Easy)
- 202. Happy Number (Top Medium)
Enforce uniqueness constraints¶
Reject repeated values within a normalized or local constraint region.
- 36. Valid Sudoku (Top Easy)
- 929. Unique Email Addresses (Google Prep)
- 301. Remove Invalid Parentheses (Top Hard)
- 1203. Sort Items by Groups Respecting Dependencies (Graph Theory)
Track visited graph states¶
Mark states at discovery so they are not explored repeatedly.
- BFS (DSA)
- DFS (DSA)
- 127. Word Ladder (Top Hard)
- 1129. Shortest Path with Alternating Colors (Graph Theory)
- 863. All Nodes Distance K in Binary Tree (Graph Theory)
- 864. Shortest Path to Get All Keys (Graph Theory)
- 417. Pacific Atlantic Water Flow (Graph Theory)
- 721. Accounts Merge (Graph Theory)
Membership-driven sequence scans¶
Start at sequence boundaries and extend while successors are present.
- 128. Longest Consecutive Sequence (Top Hard)
Dictionary membership¶
Use a set to validate candidate words or scheduled dates.
- 139. Word Break (Top Hard)
- 140. Word Break II (Top Hard)
- 983. Minimum Cost For Tickets (Dynamic Programming)
Ancestor membership¶
Store one node's ancestors to find a shared ancestor efficiently.
- 236. Lowest Common Ancestor of a Binary Tree (Top Hard)
Collect neighbor properties¶
Gather values already used by neighboring vertices.
- 1042. Flower Planting With No Adjacent (Google Prep)