Distinct Subsequences

Count distinct subsequences of s that equal t — 2D DP over prefix counts.

Hard
Time O(n * m) Space O(m)
LeetCode
3 min read
dp string

Problem#

Return the number of distinct subsequences of s that equal t.

Examples#

  • s = "rabbbit", t = "rabbit"3
  • s = "babgbag", t = "bag"5

Constraints#

  • 1 <= n, m <= 1000.

Approach#

dp[j] = number of ways t[:j] appears in the processed prefix of s. For each s[i], update j from m down to 1: if s[i] == t[j-1], dp[j] += dp[j-1].

Solution#

Distinct Subsequences
class Solution {
public:
int numDistinct(string s, string t) {
int n = s.size(), m = t.size();
vector<unsigned long long> dp(m + 1, 0);
dp[0] = 1;
for (int i = 0; i < n; ++i) {
for (int j = m; j >= 1; --j) {
if (s[i] == t[j - 1]) dp[j] += dp[j - 1];
}
}
return (int)dp[m];
}
};

Editorial#

Going right-to-left in j avoids double-counting the current s[i] for the same t[j]. The 2D DP dp[i][j] collapses to a 1D rolling array because each cell only depends on the previous row’s j-1.

Complexity#

  • Time: O(n * m).
  • Space: O(m).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.