You are given two strings `s` and `t`. A subsequence of `s` is any string obtained by deleting zero or more characters from `s` without changing the order of the remaining characters.
Return the number of distinct subsequences of `s` that are exactly equal to `t`. Two selections count as different if they choose characters from different positions in `s`, even when the resulting strings look identical.
The answer is guaranteed to fit in a signed 32-bit integer.
Example 1
Input: s = "rabbbit", t = "rabbit"
Output: 3
The target `rabbit` can be formed by keeping different combinations of the three `b` characters in `rabbbit`, giving three distinct position selections.
Example 2
Input: s = "babgbag", t = "bag"
Output: 5
There are five different ways to pick positions from `babgbag` that spell out `bag`.
Constraints
1 <= s.length, t.length <= 1000s and t consist of English letters.The answer fits within a signed 32-bit integer.See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI