> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Distinct Subsequences Python Solution

> Tested Python solution for LeetCode 115 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 115, Hard. Topics: String, Dynamic Programming. [View on LeetCode](https://leetcode.com/problems/distinct-subsequences/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 115   # by problem number
lcpy gen -s distinct_subsequences   # by problem name
```

## Problem

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.

### Examples

```
Input: s = "rabbbit", t = "rabbit"
Output: 3
Explanation:
As shown below, there are 3 ways you can generate "rabbit" from s.
rabbbit
rabbbit
rabbbit
```

```
Input: s = "babgbag", t = "bag"
Output: 5
Explanation:
As shown below, there are 5 ways you can generate "bag" from s.
babgbag
babgbag
babgbag
babgbag
babgbag
```

### Constraints

* 1 \<= s.length, t.length \<= 1000
* s and t consist of English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/distinct_subsequences/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/distinct_subsequences/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n)
    # Space: O(n) using a single rolling row
    def num_distinct(self, s: str, t: str) -> int:
        m, n = len(s), len(t)
        # dp[j] = number of ways to form t[:j] from the s prefix seen so far
        dp = [0] * (n + 1)
        dp[0] = 1  # empty t matches any s prefix exactly once
        for i in range(1, m + 1):
            # Iterate j backwards so dp[j-1] is still from the previous row
            for j in range(n, 0, -1):
                if s[i - 1] == t[j - 1]:
                    dp[j] += dp[j - 1]
        return dp[n]
```

## Complexity

| Time      | Space                           |
| --------- | ------------------------------- |
| O(m \* n) | O(n) using a single rolling row |

## Tags

[NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
