## Guessing
Lets start with `rabbbit` and `rabbit`.
To make the problem easier, start from one char, `distinct subsequences` of `rabbbit` and `r`.
`distinct subsequences` below, by eyeball:
* `r` and `r` has 1 `distinct subsequences`
* `a` and `r` has 0 `distinct subsequences`
These two are easy to be understood.
* `ra` and `r` has 1 `distinct subsequences`
* `rab` and `r` has 1 `distinct subsequences`
* `rabbbbbbbbbbbb` and `r` has 1 `distinct subsequences`
These three show that `r` plus any junk chars will not change `distinct subsequences`
* `rr` and `r` has 2 `distinct subsequences`
* `rar` and `r` has 2 `distinct subsequences`
* `rara` and `r` has 2 `distinct subsequences`
* `rarar` and `r` has 3 `distinct subsequences`
`rar` and `r` is same as `rr` and `r`, because `a` there has nothing to do with the number of `distinct subsequences`, it is a junk like above.
In another word, `rar` and `r` can be converted to `ra` + `r` and `r`, that is `1 + 1`.
### Put them into table
the value `P[i][j]` means that `S[0..i]` and `T[0..j]` has `P[i][j]` `distinct subsequences`.
the table with only one char
```
+---+---+---+---+---+---+---+---+
| | r | a | b | b | b | i | t |
+---+---+---+---+---+---+---+---+
| r | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
```
Enlarge the table
```
+---+---+---+---+---+---+---+---+
| | r | a | b | b | b | i | t |
+---+---+---+---+---+---+---+---+
| r | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
| a | | | | | | | |
+---+---+---+---+---+---+---+---+
| b | | | | | | | |
+---+---+---+---+---+---+---+---+
| b | | | | | | | |
+---+---+---+---+---+---+---+---+
| i | | | | | | | |
+---+---+---+---+---+---+---+---+
| t | | | | | | | |
+---+---+---+---+---+---+---+---+
```
Some grids here are easy to be filled, the grids which `j > i`.
That is, T is longer than S, there will be no `distinct subsequences`.
```
+---+---+---+---+---+---+---+---+
| | r | a | b | b | b | i | t |
+---+---+---+---+---+---+---+---+
| r | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
| a | 0 | | | | | | |
+---+---+---+---+---+---+---+---+
| b | 0 | 0 | | | | | |
+---+---+---+---+---+---+---+---+
| b | 0 | 0 | 0 | | | | |
+---+---+---+---+---+---+---+---+
| i | 0 | 0 | 0 | 0 | | | |
+---+---+---+---+---+---+---+---+
| t | 0 | 0 | 0 | 0 | 0 | | |
+---+---+---+---+---+---+---+---+
```
`j == i` grids are easy too, the S and T must be the same if it wants to have 1 `distinct subsequences`.
```
+---+---+---+---+---+---+---+---+
| | r | a | b | b | b | i | t |
+---+---+---+---+---+---+---+---+
| r | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
| a | 0 | 1 | | | | | |
+---+---+---+---+---+---+---+---+
| b | 0 | 0 | 1 | | | | |
+---+---+---+---+---+---+---+---+
| b | 0 | 0 | 0 | 1 | | | |
+---+---+---+---+---+---+---+---+
| i | 0 | 0 | 0 | 0 | 0 | | |
+---+---+---+---+---+---+---+---+
| t | 0 | 0 | 0 | 0 | 0 | 0 | |
+---+---+---+---+---+---+---+---+
```
This pictrue is just encouraging you, as there is only a few grids left to be filled.
### `rab` and `ra`
This problem looks familiar. It is the `ra` and `r`.
as `b` is a junk char, `rab` should have the same number as `ra` and `ra`.
Then the table is like
```
+---+---+---+---+---+---+---+---+
| | r | a | b | b | b | i | t |
+---+---+---+---+---+---+---+---+
| r | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
| a | 0 | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
| b | 0 | 0 | 1 | | | | |
+---+---+---+---+---+---+---+---+
| b | 0 | 0 | 0 | 1 | | | |
+---+---+---+---+---+---+---+---+
| i | 0 | 0 | 0 | 0 | 0 | | |
+---+---+---+---+---+---+---+---+
| t | 0 | 0 | 0 | 0 | 0 | 0 | |
+---+---+---+---+---+---+---+---+
```
### `rabb` and `rab`
This time, add a new `b` to both `rab` and `ra`.
#### Use the new `b` in subsequences
This time they have same letter, `b` , at the end. It is not a junk.
This time, remove `b` from both S and T, then they become `rab` and `ra`.
that is, this will take `rab` and `ra` `distinct subsequences`.
#### Dont use the new `b`
Treat `b` as junk, it has `rab` and `rab` `distinct subsequences`.
#### Sum up two choices
Use or not will result two set of `distinct subsequences`, so sum them up.
`rabb` and `rab` = `rab` and `ra` + `rab` and `rab`.
```
+---+---+---+---+---+---+---+---+
| | r | a | b | b | b | i | t |
+---+---+---+---+---+---+---+---+
| r | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
| a | 0 | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
| b | 0 | 0 | 1 | 2 | | | |
+---+---+---+---+---+---+---+---+
| b | 0 | 0 | 0 | 1 | | | |
+---+---+---+---+---+---+---+---+
| i | 0 | 0 | 0 | 0 | 0 | | |
+---+---+---+---+---+---+---+---+
| t | 0 | 0 | 0 | 0 | 0 | 0 | |
+---+---+---+---+---+---+---+---+
```
## General form
the grid `P[i][j]` has 1 choice when `S[i] != T[j]`, this only adds some junk chars.
```
P[i][j] = P[i - 1][j]
```
the grid `P[i][j]` has 2 choice when `S[i] == T[j]`
```
P[i][j] =
P[i - 1][j] // adds as junk chars.
+
P[i - 1][j - 1] // use the new char in the new subsequence
```
Fill up the table and the bottom right grid is the answer.
没有合适的资源?快使用搜索试试~ 我知道了~
Leetcode所有题目和解答(多语言实现).rar
共972个文件
md:497个
java:259个
txt:192个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 168 浏览量
2023-07-31
13:37:54
上传
评论
收藏 11.07MB RAR 举报
温馨提示
Leetcode所有题目和解答(多语言实现) leetCode java-leetcode leetcode算法精髓C+ +语言版本.pdf leetcode- solution.pdf lintcode-cpp.pdf letcode_ _c语言版本(新版)cpp.pdf c语言版本cpp .pdf LeetCodet题解. pdf leetcode-java.pdf
资源推荐
资源详情
资源评论
收起资源包目录
Leetcode所有题目和解答(多语言实现).rar (972个子文件)
CNAME 17B
main.css 10KB
leetcode.docx 440KB
Gemfile 186B
.gitignore 209B
footer.html 5KB
solution.html 2KB
header.html 1KB
head.html 547B
index.html 423B
default.html 246B
Solution.java 5KB
Solution.java 5KB
Solution.java 5KB
Solution.java 4KB
Solution.java 3KB
Solution.java 3KB
Solution.java 3KB
Solution.java 3KB
Solution.java 3KB
Solution.java 3KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 2KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
Solution.java 1KB
共 972 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
小正太浩二
- 粉丝: 237
- 资源: 5944
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功