Hashing
Each of these has a quadratic solution that passes the small checks and a linear one that passes them all. Find the lookup that replaces the inner loop.
The lesson is Hashing.
Count pairs with a sum
Write count_pairs(nums, target) that returns how many pairs of positions i < j have
nums[i] + nums[j] == target. Repeated values make separate pairs.
count_pairs([1, 5, 7, -1, 5], 6) -> 3 (1,5) (7,-1) (1,5)
count_pairs([1, 1, 1, 1], 2) -> 6
The point is to practise counting complements with a dictionary as you go. One check has a hundred thousand numbers.
def count_pairs(nums, target):
...
Longest run of consecutive values
Write longest_consecutive(nums) that returns the length of the longest sequence of
consecutive whole numbers that all appear in nums, in any order and with repeats allowed.
longest_consecutive([100, 4, 200, 1, 3, 2]) -> 4 (1, 2, 3, 4)
longest_consecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]) -> 9
longest_consecutive([]) -> 0
The point is to practise a set that lets you start counting only at the beginning of a run. Sorting also works; the set version is linear.
def longest_consecutive(nums):
...
Same shape of letters
Two strings are isomorphic if every character of the first can be replaced to get the
second, with each character always mapping to the same character and no two
characters mapping to the same one. Write isomorphic(s, t) for strings of equal length.
isomorphic("egg", "add") -> True
isomorphic("foo", "bar") -> False
isomorphic("paper", "title") -> True
isomorphic("ab", "aa") -> False
The point is to practise maintaining a mapping in both directions.
def isomorphic(s, t):
...
First unique character
Write first_unique(text) that returns the index of the first character that appears
exactly once in text, or -1 if there is none.
first_unique("leetcode") -> 0
first_unique("loveleetcode") -> 2
first_unique("aabb") -> -1
The point is to practise two passes over the same data: count, then scan.
def first_unique(text):
...
Count runs that sum to k
Write count_runs(nums, k) that returns how many runs of consecutive items (of length
at least one) sum to exactly k. Values may be negative.
count_runs([1, 1, 1], 2) -> 2
count_runs([1, 2, 3], 3) -> 2 ([1, 2] and [3])
count_runs([1, -1, 1, -1], 0) -> 4
The point is to practise prefix sums in a dictionary: a run ending here sums to k when
an earlier prefix equals the current prefix minus k. One check has a hundred thousand
items.
def count_runs(nums, k):
...
Duplicate within k positions
Write nearby_duplicate(nums, k) that returns True if some value appears twice at
positions at most k apart.
nearby_duplicate([1, 2, 3, 1], 3) -> True
nearby_duplicate([1, 2, 3, 1, 2, 3], 2) -> False
nearby_duplicate([1, 0, 1, 1], 1) -> True
The point is to practise a dictionary from value to its last position.
def nearby_duplicate(nums, k):
...
Does the sentence follow the pattern?
Write follows(pattern, sentence) where pattern is a string of letters and sentence
is words separated by single spaces. Return True if there is a one-to-one
correspondence between letters and words.
follows("abba", "dog cat cat dog") -> True
follows("abba", "dog cat cat fish") -> False
follows("aaaa", "dog cat cat dog") -> False
follows("abc", "dog dog") -> False
The point is to practise a two-way mapping where the two sides are different kinds of thing.
def follows(pattern, sentence):
...
Common items with counts
Write intersection(a, b) that returns the values that appear in both lists, with each
value appearing as many times as it appears in both (the smaller of its two counts),
sorted ascending.
intersection([1, 2, 2, 1], [2, 2]) -> [2, 2]
intersection([4, 9, 5], [9, 4, 9, 8, 4]) -> [4, 9]
The point is to practise Counter intersection.
def intersection(a, b):
...
Can the note be made?
Write can_make(note, magazine) that returns True if every character of note can
be cut from magazine, each magazine character used at most once. Spaces count as
characters.
can_make("aa", "aab") -> True
can_make("aa", "ab") -> False
can_make("", "x") -> True
The point is to practise Counter subtraction as a containment test.
def can_make(note, magazine):
...
Longest run summing to zero
Write longest_zero_run(nums) that returns the length of the longest run of
consecutive items whose sum is zero, or 0 if there is none.
longest_zero_run([1, -1, 3, 2, -2, -3, 3]) -> 6 ([1, -1, 3, 2, -2, -3])
longest_zero_run([1, 2, 3]) -> 0
longest_zero_run([0, 0, 0]) -> 3
The point is to practise prefix sums with a dictionary of first occurrences: two equal prefix sums bracket a zero-sum run.
def longest_zero_run(nums):
...
Next: Two pointers and windows.