Distinct Subsequences
Given two strings s and t, return the number of distinct subsequences of s which equals t. A subsequence of a string is a new string formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters.

30:00

Distinct Subsequences
hard
Topics
Companies

Given two strings s and t, return the number of distinct subsequences of s which equals t. A subsequence of a string is a new string formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters.

Example 1:
Input: {"s":"rabbbit","t":"rabbit"}
Output: 3
Constraints:
  • 1s.length,t.length10001 \leq s.\text{length}, t.\text{length} \leq 1000

  • s and t consist of English letters.

Input
arr ={"s":"rabbbit","t":"rabbit"}

Initialize DP table. dp[i][0] = 1 (empty t matches any prefix).

s:
r
a
b
b
b
i
t
t:
r
a
b
b
i
t
DP Table (dp[i][j] = ways to form t[0..j-1] from s[0..i-1])
""
r
a
b
b
i
t
""
1
0
0
0
0
0
0
r
1
0
0
0
0
0
0
a
1
0
0
0
0
0
0
b
1
0
0
0
0
0
0
b
1
0
0
0
0
0
0
b
1
0
0
0
0
0
0
i
1
0
0
0
0
0
0
t
1
0
0
0
0
0
0

Distinct Subsequences: 0

Variables
No variables to display
DepthFunction Call
Stack empty
0/43