# LeetCode 115 Distinct Subsequences

Pick One

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).

Example 1:

```Input: S = "rabbbit", T = "rabbit"
Output: 3
Explanation:

As shown below, there are 3 ways you can generate "rabbit" from S.
(The caret symbol ^ means the chosen letters)

rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^```

Example 2:

```Input: S = "babgbag", T = "bag"
Output: 5
Explanation:

As shown below, there are 5 ways you can generate "bag" from S.
(The caret symbol ^ means the chosen letters)

babgbag
^^ ^
babgbag
^^    ^
babgbag
^    ^^
babgbag
^  ^^
babgbag
^^^

O(n) = n*（n-1）*(n-2)*...(n-m+1)*m

O(n)=  n * m

c++```
```class Solution {
public:
int result;
int sss[100005];//ss数组的前缀和数组
int ss[100005];//当前字符串前面满足条件的前缀字符串的个数
int numDistinct(string s, string t) {
int lens = s.length();
int lent = t.length();
memset(sss,0,sizeof(sss));

for(int j=0;j<lent;j++)
{
memset(ss,0,sizeof(ss));
for(int i=j;i<lens;i++)
{
if(s[i]==t[j])
{
ss[i]=(j==0?1:sss[i-1]);
}
}
for(int i=0;i<lens;i++)
{
if(i==0){sss[i]=ss[i];continue;}
sss[i]=sss[i-1]+ss[i];
}
}
for(int i=0;i<lens;i++)
{
result += ss[i];
}
return result;
}

};```

0 条评论

## 相关文章

### java常用对象

boolean b=Pattern.matches("(86)*0*1\\d{10}",mobile);//大陆手机号码的匹配 日期类 Date date =...

24700

34840

11420

12730

18420

17040

13030

501110

22960

10410