Skip to content

Commit 7df6aba

Browse files
committed
940 added
1 parent e2c13a5 commit 7df6aba

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [940. Distinct Subsequences II](https://leetcode.com/problems/distinct-subsequences-ii/)
2+
3+
Given a string S, count the number of distinct, non-empty subsequences of S .
4+
5+
Since the result may be large, return the answer modulo 10^9 + 7.
6+
7+
Example 1:
8+
9+
```text
10+
Input: "abc"
11+
Output: 7
12+
Explanation: The 7 distinct subsequences are "a", "b", "c", "ab", "ac", "bc", and "abc".
13+
```
14+
15+
Example 2:
16+
17+
```text
18+
Input: "aba"
19+
Output: 6
20+
Explanation: The 6 distinct subsequences are "a", "b", "ab", "ba", "aa" and "aba".
21+
```
22+
23+
Example 3:
24+
25+
```text
26+
Input: "aaa"
27+
Output: 3
28+
Explanation: The 3 distinct subsequences are "a", "aa" and "aaa".
29+
```
30+
31+
Note:
32+
33+
1. `S contains only lowercase letters.`
34+
1. `1 <= S.length <= 2000`
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package problem0940
2+
3+
func distinctSubseqII(S string) int {
4+
5+
return 0
6+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package problem0940
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// tcs is testcase slice
10+
var tcs = []struct {
11+
S string
12+
ans int
13+
}{
14+
15+
{
16+
"abc",
17+
7,
18+
},
19+
20+
{
21+
"aba",
22+
6,
23+
},
24+
25+
{
26+
"aaa",
27+
3,
28+
},
29+
30+
// 可以有多个 testcase
31+
}
32+
33+
func Test_distinctSubseqII(t *testing.T) {
34+
ast := assert.New(t)
35+
36+
for _, tc := range tcs {
37+
ast.Equal(tc.ans, distinctSubseqII(tc.S), "输入:%v", tc)
38+
}
39+
}
40+
41+
func Benchmark_distinctSubseqII(b *testing.B) {
42+
for i := 0; i < b.N; i++ {
43+
for _, tc := range tcs {
44+
distinctSubseqII(tc.S)
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)