Problem Statement
Distinct Subsequences
You get two strings, s and t. A subsequence means you take the big string s and cross out some letters (maybe none), but you keep the letters that are left in the same order. For example, "ACE" is a subsequence of "ABCDE" because you can cross out the B and the D and what is left still reads "ACE" in order. But "AEC" is not, because the letters are out of order. Your job is to count how many different ways you can cross out letters in s so that what remains spells exactly t. The puzzle promises the answer fits in a normal 32-bit integer, so you do not have to worry about giant numbers. The tool we use is a grid of counts called a DP table. DP stands for dynamic programming, which is a fancy name for one simple idea: solve tiny versions of the problem first, write the answers in a table, and build up to the big answer by reusing those small answers. Here dp[i][j] will hold the number of ways to build the first j letters of t using only the first i letters of s.
Signals to notice
Brute force first
Generate all subsequences of s and count matches. Exponential in s's length. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.
The key insight
2D DP: dp[i][j] = number of subsequences of s[0.i-1] that equal t[0.j-1]. If s[i-1] == t[j-1]: dp[i][j] = dp[i-1][j-1] (use this match) + dp[i-1][j] (skip it). If not: dp[i][j] = dp[i-1][j]. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.
Trace it on s="rabbbit", t="rabbit"
Init: dp[i][0]=1 for all i (empty target = 1 way). m=7, n=6. t='rabbit' has chars r,a,b,b,i,t at j=1..6 (note TWO b columns: j=3 and j=4). i=1 s='r' matches t[0]='r' (j=1): dp[1][1]=dp[0][1]+dp[0][0]=0+1=1. Row: [1,1,0,0,0,0,0]. i=2 s='a' matches t[1]='a' (j=2): dp[2][2]=dp[1][2]+dp[1][1]=0+1=1; dp[2][1]=1 carried. Row: [1,1,1,0,0,0,0]. i=3 s='b' (1st b) matches t[2]='b' (j=3): dp[3][3]=dp[2][3]+dp[2][2]=0+1=1. j=4 second-b col: dp[3][4]=dp[2][4]+dp[2][3]=0+0=0. Row: [1,1,1,1,0,0,0]. i=4 s='b' (2nd b): dp[4][3]=dp[3][3]+dp[3][2]=1+1=2; dp[4][4]=dp[3][4]+dp[3][3]=0+1=1. Row: [1,1,1,2,1,0,0]. i=5 s='b' (3rd b): dp[5][3]=dp[4][3]+dp[4][2]=2+1=3; dp[5][4]=dp[4][4]+dp[4][3]=1+2=3. Row: [1,1,1,3,3,0,0]. i=6 s='i' matches t[4]='i' (j=5): dp[6][5]=dp[5][5]+dp[5][4]=0+3=3. Row: [1,1,1,3,3,3,0]. i=7 s='t' matches t[5]='t' (j=6): dp[7][6]=dp[6][6]+dp[6][5]=0+3=3. Return dp[7][6]=3.
What must stay true
When characters match, you have two valid choices: use the match (consuming both characters) or skip it (only consuming s's character, keeping more flexibility). The total is the sum of both paths. As long as that statement keeps holding, you can trust the steps built on top of it.
Shape of the loop
dp[i][0] = 1 for all i # empty t: one way (delete all)
for i in 1..m:
for j in 1..n:
dp[i][j] = dp[i-1][j] # always: skip s[i-1]
if s[i-1] == t[j-1]:
dp[i][j] += dp[i-1][j-1] # also: use the match
return dp[m][n]Pseudocode only — the full worked solution lives in the Solution tab.
Easy way to go wrong
Forgetting the skip case when characters match — even when s[i] matches t[j], you might find more matches by skipping this particular occurrence of s[i]. The fix is usually to return to the meaning of each move, not just the steps themselves.