> For the complete documentation index, see [llms.txt](https://nataliekung.gitbook.io/ladder_code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nataliekung.gitbook.io/ladder_code/l4dong-tai-gui-hua/distinct-subsequences.md).

# Distinct subsequences

Given a string **S** and a string **T**, count the number of distinct subsequences of **S** which equals **T**.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,`"ACE"`is a subsequence of`"ABCDE"`while`"AEC"`is not).

Here is an example:\
**S**=`"rabbbit"`,**T**=`"rabbit"`

Return`3`.

分析

s里可以删也可以取（相等时候），就像unique path一样，几种来到当前点的路径相加。

```
 * Path[i][j] = Path[i][j-1]            (discard S[j])
 *              +     Path[i-1][j-1]    (S[j] == T[i] and we are going to use S[j])
 *                 or 0                 (S[j] != T[i] so we could not use S[j])
 * while Path[0][j] = 1 and Path[i][0] = 0.//初始化
```

```
class Solution {
    public int numDistinct(String s, String t) {
        int n = s.length();
        int m = t.length();
        if(m > n) return 0;
        int[][] f = new int[n + 1][m + 1];
        //不能在循环里初始化，是为了防止a,b的长度不足1时候。

        for(int i=0; i< n+1; i++){
            f[i][0] = 1; #对角线上的 需要给1开始。也可以是S全部删掉，至少有1种。
        }#不要初始f[0][j]因为后面无论如何f[i][j] = f[i-1][j]，该是0

        for(int i = 1; i <= n; i ++){            
            for(int j = 1; j <= m; j ++){                 
                    f[i][j] = f[i - 1][j] + (s.charAt(i-1) == t.charAt(j-1) ? f[i - 1][j - 1] : 0);                                             
            }
        }
        return f[n][m];
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nataliekung.gitbook.io/ladder_code/l4dong-tai-gui-hua/distinct-subsequences.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
