Distinct Subsequences
Learn how to solve the 'Distinct Subsequences' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Given two strings s and t, return the number of distinct subsequences of s which equals t.
A string's subsequence 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. (i.e., "ACE" is a subsequence of "ABCDE" while "AEC" is not).
Write a function numDistinct(s: str, t: str) -> int.
- •1 <= len(s), len(t) <= 1000
- •s and t consist of English letters
Examples
s = "rabbbit", t = "rabbit"
3
There are 3 ways you can generate "rabbit" from s: **rab**b**bit**, **ra**b**bbit**, **rab**bb**it**.
s = "babgbag", t = "bag"
5
There are 5 ways you can generate "bag" from s.
Need a Hint?
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.