|
| 1 | +# 115. [Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/) |
| 2 | + |
| 3 | +## Approach 1: Recursion with Memoization |
| 4 | + |
| 5 | +### Solution |
| 6 | +python |
| 7 | +```python |
| 8 | +# Time Complexity: O(n * m) |
| 9 | +# Space Complexity: O(n * m) |
| 10 | +class Solution: |
| 11 | + def numDistinct(self, s: str, t: str) -> int: |
| 12 | + # Memoization table |
| 13 | + memo = [[-1 for _ in range(len(t))] for _ in range(len(s))] |
| 14 | + return self.numDistinctHelper(s, t, 0, 0, memo) |
| 15 | + |
| 16 | + def numDistinctHelper(self, s: str, t: str, sIndex: int, tIndex: int, memo: list) -> int: |
| 17 | + # If t is exhausted, one subsequence is found |
| 18 | + if tIndex == len(t): |
| 19 | + return 1 |
| 20 | + # If s is exhausted first, no subsequence can be formed |
| 21 | + if sIndex == len(s): |
| 22 | + return 0 |
| 23 | + # Check memoization table |
| 24 | + if memo[sIndex][tIndex] != -1: |
| 25 | + return memo[sIndex][tIndex] |
| 26 | + |
| 27 | + # If characters match, both decisions can be made |
| 28 | + if s[sIndex] == t[tIndex]: |
| 29 | + memo[sIndex][tIndex] = self.numDistinctHelper(s, t, sIndex + 1, tIndex + 1, memo) + \ |
| 30 | + self.numDistinctHelper(s, t, sIndex + 1, tIndex, memo) |
| 31 | + else: |
| 32 | + # If characters don't match, skip the current character in s |
| 33 | + memo[sIndex][tIndex] = self.numDistinctHelper(s, t, sIndex + 1, tIndex, memo) |
| 34 | + |
| 35 | + return memo[sIndex][tIndex] |
| 36 | +``` |
| 37 | + |
| 38 | +## Approach 2: Dynamic Programming |
| 39 | + |
| 40 | +### Solution |
| 41 | +python |
| 42 | +```python |
| 43 | +# Time Complexity: O(n * m) |
| 44 | +# Space Complexity: O(n * m) |
| 45 | +class Solution: |
| 46 | + def numDistinct(self, s: str, t: str) -> int: |
| 47 | + # DP table |
| 48 | + dp = [[0] * (len(t) + 1) for _ in range(len(s) + 1)] |
| 49 | + |
| 50 | + # Base case: empty t can be formed by all possible substrings of s |
| 51 | + for i in range(len(s) + 1): |
| 52 | + dp[i][len(t)] = 1 |
| 53 | + |
| 54 | + # Fill the table in reverse order |
| 55 | + for i in range(len(s) - 1, -1, -1): |
| 56 | + for j in range(len(t) - 1, -1, -1): |
| 57 | + if s[i] == t[j]: |
| 58 | + # Both using the character and ignoring it |
| 59 | + dp[i][j] = dp[i + 1][j + 1] + dp[i + 1][j] |
| 60 | + else: |
| 61 | + # Ignore the character in s |
| 62 | + dp[i][j] = dp[i + 1][j] |
| 63 | + |
| 64 | + # Result is the number of ways to form t[0...m] from s[0...n] |
| 65 | + return dp[0][0] |
| 66 | +``` |
| 67 | + |
| 68 | +## Approach 3: Dynamic Programming with Space Optimization |
| 69 | + |
| 70 | +### Solution |
| 71 | +python |
| 72 | +```python |
| 73 | +# Time Complexity: O(n * m) |
| 74 | +# Space Complexity: O(m) |
| 75 | +class Solution: |
| 76 | + def numDistinct(self, s: str, t: str) -> int: |
| 77 | + # Previous and current row for space optimization |
| 78 | + prev = [0] * (len(t) + 1) |
| 79 | + curr = [0] * (len(t) + 1) |
| 80 | + |
| 81 | + # Base case: empty t can be formed by all possible substrings of s |
| 82 | + for i in range(len(s) + 1): |
| 83 | + prev[len(t)] = 1 |
| 84 | + |
| 85 | + # Fill the table in reverse order |
| 86 | + for i in range(len(s) - 1, -1, -1): |
| 87 | + for j in range(len(t) - 1, -1, -1): |
| 88 | + if s[i] == t[j]: |
| 89 | + curr[j] = prev[j + 1] + prev[j] |
| 90 | + else: |
| 91 | + curr[j] = prev[j] |
| 92 | + # Move current row to previous for next iteration |
| 93 | + prev = curr[:] |
| 94 | + |
| 95 | + # Result is the number of ways to form t[0...m] from s[0...n] |
| 96 | + return prev[0] |
| 97 | +``` |
| 98 | + |
0 commit comments