###### Hard-题目12：115. Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S.

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.

dp[0][0]显然等价于s[0]==t[0],而先初始化j=0的情况，因为这时只有一个字符，扫描s，只要s[i]==t[0]，则取出的方法数+1，否则不变（很好理解，因为多出的字符与t[0]不一样，这个多出的字符没有任何意义）。

public class Solution {
public int numDistinct(String s, String t) {
int slen = s.length(),tlen = t.length();
if(slen<tlen)
return 0;
else if(slen == tlen)
return s.equals(t)?1:0;
else {
int[][] dp = new int[slen][tlen];
dp[0][0] = s.charAt(0)==t.charAt(0)?1:0;
for(int i = 1;i<slen;i++) {
if(s.charAt(i)==t.charAt(0))
dp[i][0] = dp[i-1][0]+1;
else
dp[i][0] = dp[i-1][0];
}
for(int j = 1;j<tlen;j++) {
for(int i = j;i<slen;i++) {
if(s.charAt(i) == t.charAt(j))
dp[i][j] = dp[i-1][j]+dp[i-1][j-1];
else
dp[i][j] = dp[i-1][j];
}
}
return dp[slen-1][tlen-1];
}
}
}

16ms,beats 62.01%,众数20ms,14.82%
Cmershen的碎碎念：

#### LeetCode(115) Distinct Subsequences

2015-01-21 04:02:50

#### [leetcode-115]Distinct Subsequences(java)

2015-09-03 23:17:15

#### LeetCode（115） Distinct Subsequences

2015-12-26 14:36:37

#### 115. Distinct Subsequences（互异子序列）

2018-01-05 21:03:03

#### Sorting by Subsequences CodeForces - 844C

2018-02-11 21:56:38

#### Leetcode Distinct Subsequences 解题报告

2014-02-23 21:51:17

#### Distinct Subsequences 解题报告

2014-10-19 16:36:13

#### Distinct Subsequences -- LeetCode

2014-04-13 09:19:57

#### CSU 1354 Distinct Subsequences 求不相同子序列的和 dp

2015-01-24 19:11:23

#### [LeetCode] 115. Distinct Subsequences

2016-08-27 11:54:31

## 不良信息举报

Hard-题目12：115. Distinct Subsequences