Distinct Subsequences Problem
Description
LeetCode Problem 115.
Given two strings s and t, return the number of distinct subsequences of s which equals t.
A string's subsequence is a new string formed from the original string by deleting some (can be none) of the characters without disturbing the remaining characters' relative positions. (i.e., "ACE" is a subsequence of "ABCDE" while "AEC" is not).
The test cases are generated so that the answer fits on a 32-bit signed integer.
Example 1:
Input: s = "rabbbit", t = "rabbit"
Output: 3
Explanation:
As shown below, there are 3 ways you can generate "rabbit" from S.
rabbbit
rabbbit
rabbbit
Example 2:
Input: s = "babgbag", t = "bag"
Output: 5
Explanation:
As shown below, there are 5 ways you can generate "bag" from S.
babgbag
babgbag
babgbag
babgbag
babgbag
Constraints:
- 1 <= s.length, t.length <= 1000
- s and t consist of English letters.
Solution
Approach: Dynamic Programming over Prefixes of t
Listing every subsequence of s is exponential. Instead, read s from left to right and keep, for every prefix of t, the number of ways to form it from the characters read so far.
Let ways[j] be the number of ways to form t[0..j) (the first j characters of t). Before reading anything, ways[0] = 1, since the empty prefix is formed in exactly one way, by choosing nothing, and every other entry is 0.
When the next character ch of s is read, each way to form t[0..j) either:
- does not use
ch: these are theways[j]ways that already existed, or - uses
chas its last character. This is only possible whent[j-1] == ch, and then there areways[j-1]of them, one for each way to form the rest from earlier characters.
So for every j with t[j-1] == ch, add ways[j-1] to ways[j]. The answer is ways[m], where m is the length of t.
Update j from m down to 1. Then ways[j-1] still holds the count from before ch when ways[j] reads it. Going upwards would let one character of s stand for two consecutive characters of t. For example, with s = "a" and t = "aa", the answer would come out as 1 instead of 0.
The answer fits in 32 bits, but the counts for shorter prefixes of t can be far larger (for s = 1000 a's and t = 999 a's, the count for 500 a's has about 300 digits). Python's integers have no limit. In the other languages the code lets the counts wrap around (unsigned in C++, | 0 in JavaScript). Since the DP only adds, the wrapped value of ways[m] equals the true answer modulo 232 (264 for Go's int), and that is the answer itself.
Complexity: O(n · m) time and O(m) space, where n and m are the lengths of s and t.
class Solution:
def numDistinct(self, s: str, t: str) -> int:
m = len(t)
# ways[j] = number of ways to form t[:j] from the characters of s read so far
ways = [0] * (m + 1)
ways[0] = 1 # the empty prefix is formed in exactly one way
for ch in s:
# Go backwards so that ways[j - 1] is still the count from before this character.
for j in range(m, 0, -1):
if t[j - 1] == ch:
ways[j] += ways[j - 1] # use ch as the j-th character of t
return ways[m]
Now write it from memory
Close the write-up and solve it yourself against the tests.