Distinct Subsequences
1 approach, code in every language
Problem description
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.
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Ways to Spell the Motif is our own problem on the technique behind Distinct Subsequences: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free