Distinct Subsequences
Distinct Subsequences is LeetCode 115 (Hard). Given strings s and t, count the subsequences of s that equal t. A subsequence keeps some characters of s in their original order, and two subsequences count as different when they use different positions of s, even if they spell the same thing.
- Both strings have up to 1000 letters, so about 10⁶ steps is fine.
- The final answer fits in a 32-bit integer, but the counts along the way may not.
- 1 <= s.length, t.length <= 1000
- s and t consist of English letters.
Intuition
Let ways[j] be the number of ways to spell the first j letters of t using the part of s read so far. Before anything is read, ways[0] = 1 (spell nothing by choosing nothing) and every other entry is 0.
When the next letter of s equals t[j − 1], every way to spell the first j − 1 letters can be finished with this letter, so ways[j] grows by ways[j − 1]. The old ways, which skip this letter, still count as well, which is why it is an addition and not a replacement.
Read s once, updating ways after each letter, and ways[len(t)] is the answer.
Counting the ways to pick positions from one sequence so they spell another is a two-string DP whose state is how much of the target is spelled so far. The choice to use or skip each letter turns into an addition.
Approach
Before reading on, list by hand every way to pick bag out of babgbag, writing each one as the positions it uses. Then look at how the count for ba feeds the count for bag. Aim for O(|s| · |t|) time and O(|t|) space.
Start with one way to spell nothing
Create ways with len(t) + 1 entries: ways[0] = 1 and the rest 0. The empty prefix of t is spelled exactly one way, by choosing nothing, and longer prefixes have no ways yet because no letter of s has been read.
Read s one letter at a time
For each letter ch of s, visit j from len(t) down to 1 and, wherever t[j - 1] == ch, add ways[j - 1] to ways[j]. Going downwards means ways[j - 1] still holds its count from before this letter.
Why each choice is counted exactly once
A set of positions that spells the first j letters of t is added at one moment only: when its last letter is read, through the count for its first j − 1 letters. Two different sets differ in some position, so they are added along different paths and never merge.
Code in Python, C++ and Java
Common pitfalls
Updating the row left to right
for j in range(1, n + 1):
for j in range(n, 0, -1):
Then ways[j - 1] may already include the current letter, so one letter of s fills two positions of t. On s = "a", t = "aa" the single a first sets ways[1] = 1 and then reuses itself for ways[2], returning 1 instead of 0.
Signed counters in C++
vector<long long> ways(n + 1, 0);
vector<unsigned long long> ways(n + 1, 0);
Middle entries can dwarf the answer. With s = 1000 copies of a and t = 500 copies of a then b, the answer is 0 but ways[500] passes 10²⁹⁹, and signed overflow is undefined behaviour, which the online judge reports as a runtime error. Unsigned numbers wrap around modulo 2⁶⁴ instead, and since every update is an addition, the wrapped final value still equals the true answer when that answer is below 2³¹. Java's long wraps the same way, so it is safe as written.
Edge cases
t longer than s, e.g. s = "ab", t = "abc"ways[j] can only become non-zero after j letters of s have matched in order, and s has too few letters, so the answer stays 0.
Complexity
s scans all of t once. The single row of len(t) + 1 counts replaces the full table, because each row of that table only reads the row above it.Distinct Subsequences FAQ
What does the full two-dimensional table look like?
dp[i][j] is the number of ways to spell the first j letters of t from the first i letters of s, with dp[i][0] = 1. Then dp[i][j] = dp[i − 1][j], plus dp[i − 1][j − 1] when s[i − 1] == t[j − 1]. The one-row version is this table with each row written over the one above it.
How does the recursive version work?
count(i, j) is the number of ways to spell t[j:] from s[i:]. It returns 1 when j == len(t) and 0 when s runs out first. Otherwise it is count(i + 1, j), plus count(i + 1, j + 1) when the letters match. With memoisation it is also O(|s| · |t|) time, but its cache needs O(|s| · |t|) memory.