Distinct Subsequences
Count the ways a word can be picked out of a longer string
DSA, Hard, 2-D DP
Which structure or algorithm, and what does it cost.
You get two strings, source and target. Count the ways to pick letters from source, keeping their order, so that the picked letters spell target. Two ways are different if they pick different places, even when the picked letters look the same. Return the number of ways.
Examples
Constraints
- 0 <= length of
source<= 60. - 0 <= length of
target<= 60. - Both strings use lowercase English letters.
- The answer fits in a 64-bit integer.
Your answer must contain
- The simple method and why it is too slow; the better method; the best method and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n * m) time and O(m) memory, where n and m are the two lengths.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Look at the last letter of target. If the last letter of source is different, what happens to it? If it is the same, what two choices do you have?
Hint 2 the approach
Define the number of ways for every pair of prefixes: the first i letters of source and the first j letters of target. Each uses shorter pairs.
Hint 3 the key step
On a match, add the ways that use this letter to the ways that skip it. Otherwise only skip. Update the list from the end.
Walkthrough
Full walkthrough brute force to best
Brute force
Choose every set of m places in source and test whether they spell target. There are C(n, m) sets, up to about 1.2 * 10^17 for n = 60, so this is too slow.
Better method
Recursion with memory. Let ways(i, j) be the number of ways to spell the first j letters of target from the first i letters of source. If the last letters match, it is ways(i - 1, j - 1) + ways(i - 1, j). Otherwise it is ways(i - 1, j). Store each pair. Every pair is solved once, so the time and memory are both O(n * m).
Best method
Keep a list ways, where ways[j] is the number of ways to spell the first j letters of target from the source letters read so far. Start with ways[0] = 1, because the empty target has one way, and 0 elsewhere. Read the source one letter ch at a time. For each j from the end of the target down to 1, if target[j - 1] equals ch, add ways[j - 1] to ways[j]. Adding ways[j - 1] counts the ways that use this letter as the last letter of the prefix. The old ways[j] stays, which counts the ways that skip this letter. Going downward matters: it keeps the update from using one source letter for two target letters. The answer is ways[m].
Hand trace
Run on source = "banana", target = "ban". The list is ways[0] to ways[3], and starts as [1, 0, 0, 0]. After b: [1, 1, 0, 0]. After a: [1, 1, 1, 0]. After n: [1, 1, 1, 1]. After a: [1, 1, 2, 1]. After n: [1, 1, 2, 3]. After a: [1, 1, 3, 3]. The answer is ways[3], 3.
Code
def count_picks(source, target):
ways = [1] + [0] * len(target) # ways[j]: ways to spell target[:j] from the source letters read so far
for ch in source:
for j in range(len(target), 0, -1):
if target[j - 1] == ch:
ways[j] += ways[j - 1]
return ways[len(target)]Time and memory
O(n * m) time, one step per source letter and target place. O(m) memory for the list.
Common mistakes
- Updating the list from left to right. One source letter is then used for two target letters. With source "a" and target "aa" it gives 1, not 0.
- Counting distinct strings instead of ways. With source "aaaa" and target "aa" the answer is 6, one for each pair of places.
- Starting ways[0] at 0. The empty prefix of the target has exactly one way, so every count stays 0 otherwise.