Given two strings s and t, return the number of distinct subsequences of s which equals t.
The test cases are generated so that the answer fits on a 32-bit signed integer.
Example 1:
Input: s = "rabbbit", t = "rabbit" Output: 3 Explanation: As shown below, there are 3 ways you can generate "rabbit" from s.rabbbitrabbbitrabbbit
Example 2:
Input: s = "babgbag", t = "bag" Output: 5 Explanation: As shown below, there are 5 ways you can generate "bag" from s.babgbagbabgbagbabgbagbabgbagbabgbag
Constraints:
1 <= s.length, t.length <= 1000s and t consist of English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a long master text and a shorter target word, and you want to find out how many different ways you can spell out the target word using the letters from the master text in order. The brute force approach works by exploring every single pathway of matching letters from the master text to the target word.
Here's how the algorithm would work step-by-step:
class Solution:
def numDistinct(self, master_text: str, target_word: str) -> int:
def count_subsequences(master_index: int, target_index: int) -> int:
# If we matched the entire target word, we found one valid combination path
if target_index == len(target_word):
return 1
# If master text is exhausted before target, this path cannot form the word
if master_index == len(master_text):
return 0
current_ways = 0
# If characters match, branch into using this character or skipping it
if master_text[master_index] == target_word[target_index]:
current_ways += count_subsequences(master_index + 1, target_index + 1)
current_ways += count_subsequences(master_index + 1, target_index)
else:
# If characters do not match, we must skip the master text character
current_ways += count_subsequences(master_index + 1, target_index)
return current_ways
return count_subsequences(0, 0)Instead of blindly trying every possible way to spell the target word, the optimal approach builds the solution step by step using a clever memory shortcut. It solves smaller pieces of the puzzle first, saving those answers so they can be reused whenever the same situation appears again.
Here's how the algorithm would work step-by-step:
def numDistinct(source_phrase, target_word):
source_length = len(source_phrase)
target_length = len(target_word)
dynamic_table = [[0] * (target_length + 1) for _ in range(source_length + 1)]
for source_index in range(source_length + 1):
dynamic_table[source_index][0] = 1
# We walk backward through both strings to evaluate character matches and skip choices.
for source_index in range(1, source_length + 1):
for target_index in range(1, target_length + 1):
if source_phrase[source_index - 1] == target_word[target_index - 1]:
# A match allows us to either use the letter or skip it for alternative paths.
dynamic_table[source_index][target_index] = dynamic_table[source_index - 1][target_index - 1] + dynamic_table[source_index - 1][target_index]
else:
# Mismatched letters force us to bypass the current source character entirely.
dynamic_table[source_index][target_index] = dynamic_table[source_index - 1][target_index]
return dynamic_table[source_length][target_length]| Case | How to Handle |
|---|---|
| String t is empty | The dynamic programming table should initialize the base case where an empty t can be formed by any prefix of s in exactly 1 way. |
| String s is empty while t is non-empty | The algorithm will correctly return 0 because an empty string cannot form a non-empty target string. |
| Both strings s and t are empty | The base case initialization correctly returns 1 as an empty string can match another empty string in exactly 1 way. |
| String s is shorter than string t | The loops will naturally result in a count of 0 since a shorter string cannot contain a longer subsequence. |
| Strings contain all identical characters | The dynamic programming transition correctly accumulates combinations using binomial coefficients or summation over previous matches. |
| Target string t is longer than string s | Our length check and state transitions will evaluate to 0 because matching requires sufficient characters in s. |
| Integer overflow from a very large number of distinct subsequences | We must use 64-bit integers or modulo arithmetic if specified by the problem constraints to prevent arithmetic overflow. |
| Maximum length constraints up to 1000 characters | A naive recursive solution would hit exponential time complexity, so we use an O(mn) dynamic programming table to ensure efficient scaling. |