Thirteen problems that drill the hash map & frequency pattern from Lesson 6. The reps are the point — read the approach, try it yourself first, then check the code. Each solution here was actually run and tested.
How to practise. The hash-map move is almost always “trade memory for time”: store what you’ve seen so a future lookup is O(1). For each problem, name what you put in the map (value→index? char→count? prefix-sum→frequency?) before writing a line. That one decision is the whole solution.
The 13 problems
- 1Two Sum (LeetCode 1)EasyFind two indices whose values add to a target: remember each value's index and look up its complement.arrow_forward
- 2Valid Anagram (LeetCode 242)EasyCheck whether two strings use exactly the same letters: compare their character counts.arrow_forward
- 3Contains Duplicate (LeetCode 217)EasyReport whether any value appears twice: a set drops repeats, so compare its size with the list's.arrow_forward
- 4Ransom Note (LeetCode 383)EasyCan the note be cut from the magazine? Subtract letter counts and check nothing is left short.arrow_forward
- 5First Unique Character in a String (LeetCode 387)EasyFind the index of the first character that appears once: count everything, then scan in order.arrow_forward
- 6Majority Element (LeetCode 169)EasyFind the value that fills more than half the array: count it with a map, or cancel votes in O(1) space.arrow_forward
- 7Isomorphic Strings (LeetCode 205)EasyCheck if one string's letters can be renamed into the other's: keep maps in both directions.arrow_forward
- 8Group Anagrams (LeetCode 49)MediumBucket words that are anagrams of each other: use each word's sorted letters as a dictionary key.arrow_forward
- 9Top K Frequent Elements (LeetCode 347)MediumReturn the k most common values: count with a hash map, then pick the k largest counts.arrow_forward
- 10Longest Consecutive Sequence (LeetCode 128)MediumFind the longest run of consecutive integers in O(n): put them in a set and only count from run starts.arrow_forward
- 11Subarray Sum Equals K (LeetCode 560)MediumCount subarrays summing to k: each is a difference of prefix sums, so count earlier prefixes equal to s − k.arrow_forward
- 12Valid Sudoku (LeetCode 36)MediumCheck a partly filled Sudoku for repeats: record each digit as row, column and box keys in one set.arrow_forward
- 13LRU Cache (LeetCode 146)MediumBuild a fixed-size cache that evicts the least recently used key: a hash map plus a recency-ordered list.arrow_forward