Skip to content

Commit e41e252

Browse files
committed
feat: add solutions to lc problem: No.0940
No.0940.Distinct Subsequences II
1 parent f744bd3 commit e41e252

File tree

6 files changed

+366
-0
lines changed

6 files changed

+366
-0
lines changed

solution/0900-0999/0940.Distinct Subsequences II/README.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,186 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:动态规划**
59+
60+
定义 $dp[i]$ 表示以 $s[i]$ 结尾的不同子序列的个数。由于 $s$ 中只包含小写字母,因此我们可以直接创建一个长度为 $26$ 的数组。初始时 $dp$ 所有元素均为 $0$。答案为 $\sum_{i=0}^{25}dp[i]$。
61+
62+
遍历字符串 $s$,对于每个位置的字符 $s[i]$,我们需要更新以 $s[i]$ 结尾的不同子序列的个数,此时 $dp[i]=\sum_{j=0}^{25}dp[j]+1$。其中 $\sum_{j=0}^{25}dp[j]$ 是此前我们已经计算出所有不同子序列的个数,而 $+1$ 是指 $s[i]$ 本身也可以作为一个子序列。
63+
64+
最后,我们需要对 $dp$ 中的所有元素求和,再对 $10^9+7$ 取余,即为答案。
65+
66+
时间复杂度 $O(n\times C)$,其中 $n$ 是字符串 $s$ 的长度,$C$ 是字符集的大小,本题中 $C=26$。空间复杂度 $O(C)$。
67+
68+
**方法二:优化的动态规划**
69+
70+
在方法一的基础上,我们还可以维护当前 $dp$ 数组中所有元素的和 $ans$,这样我们每次更新 $dp[i]$ 时,只需要将 $dp[i]$ 加上 $ans-dp[i]+1$ 即可。
71+
72+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。
73+
5874
<!-- tabs:start -->
5975

6076
### **Python3**
6177

6278
<!-- 这里可写当前语言的特殊实现逻辑 -->
6379

6480
```python
81+
class Solution:
82+
def distinctSubseqII(self, s: str) -> int:
83+
mod = 10**9 + 7
84+
n = len(s)
85+
dp = [[0] * 26 for _ in range(n + 1)]
86+
for i, c in enumerate(s, 1):
87+
k = ord(c) - ord('a')
88+
for j in range(26):
89+
if j == k:
90+
dp[i][j] = sum(dp[i - 1]) % mod + 1
91+
else:
92+
dp[i][j] = dp[i - 1][j]
93+
return sum(dp[-1]) % mod
94+
```
6595

96+
```python
97+
class Solution:
98+
def distinctSubseqII(self, s: str) -> int:
99+
mod = 10**9 + 7
100+
dp = [0] * 26
101+
for c in s:
102+
i = ord(c) - ord('a')
103+
dp[i] = sum(dp) % mod + 1
104+
return sum(dp) % mod
105+
```
106+
107+
```python
108+
class Solution:
109+
def distinctSubseqII(self, s: str) -> int:
110+
mod = 10**9 + 7
111+
dp = [0] * 26
112+
ans = 0
113+
for c in s:
114+
i = ord(c) - ord('a')
115+
add = ans - dp[i] + 1
116+
ans = (ans + add) % mod
117+
dp[i] += add
118+
return ans
66119
```
67120

68121
### **Java**
69122

70123
<!-- 这里可写当前语言的特殊实现逻辑 -->
71124

72125
```java
126+
class Solution {
127+
private static final int MOD = (int) 1e9 + 7;
128+
129+
public int distinctSubseqII(String s) {
130+
int[] dp = new int[26];
131+
for (int i = 0; i < s.length(); ++i) {
132+
int j = s.charAt(i) - 'a';
133+
dp[j] = sum(dp) + 1;
134+
}
135+
return sum(dp);
136+
}
137+
138+
private int sum(int[] arr) {
139+
int x = 0;
140+
for (int v : arr) {
141+
x = (x + v) % MOD;
142+
}
143+
return x;
144+
}
145+
}
146+
```
147+
148+
```java
149+
class Solution {
150+
private static final int MOD = (int) 1e9 + 7;
151+
152+
public int distinctSubseqII(String s) {
153+
int[] dp = new int[26];
154+
int ans = 0;
155+
for (int i = 0; i < s.length(); ++i) {
156+
int j = s.charAt(i) - 'a';
157+
int add = (ans - dp[j] + 1) % MOD;
158+
ans = (ans + add) % MOD;
159+
dp[j] = (dp[j] + add) % MOD;
160+
}
161+
return (ans + MOD) % MOD;
162+
}
163+
}
164+
```
165+
166+
### **C++**
167+
168+
```cpp
169+
class Solution {
170+
public:
171+
const int mod = 1e9 + 7;
172+
173+
int distinctSubseqII(string s) {
174+
vector<long> dp(26);
175+
for (char& c : s) {
176+
int i = c - 'a';
177+
dp[i] = accumulate(dp.begin(), dp.end(), 1l) % mod;
178+
}
179+
return accumulate(dp.begin(), dp.end(), 0l) % mod;
180+
}
181+
};
182+
```
183+
184+
```cpp
185+
class Solution {
186+
public:
187+
const int mod = 1e9 + 7;
188+
189+
int distinctSubseqII(string s) {
190+
vector<long> dp(26);
191+
long ans = 0;
192+
for (char& c : s) {
193+
int i = c - 'a';
194+
long add = ans - dp[i] + 1;
195+
ans = (ans + add + mod) % mod;
196+
dp[i] = (dp[i] + add) % mod;
197+
}
198+
return ans;
199+
}
200+
};
201+
```
202+
203+
### **Go**
204+
205+
```go
206+
func distinctSubseqII(s string) int {
207+
const mod int = 1e9 + 7
208+
sum := func(arr []int) int {
209+
x := 0
210+
for _, v := range arr {
211+
x = (x + v) % mod
212+
}
213+
return x
214+
}
215+
216+
dp := make([]int, 26)
217+
for _, c := range s {
218+
c -= 'a'
219+
dp[c] = sum(dp) + 1
220+
}
221+
return sum(dp)
222+
}
223+
```
73224

225+
```go
226+
func distinctSubseqII(s string) int {
227+
const mod int = 1e9 + 7
228+
dp := make([]int, 26)
229+
ans := 0
230+
for _, c := range s {
231+
c -= 'a'
232+
add := ans - dp[c] + 1
233+
ans = (ans + add) % mod
234+
dp[c] = (dp[c] + add) % mod
235+
}
236+
return (ans + mod) % mod
237+
}
74238
```
75239

76240
### **...**

solution/0900-0999/0940.Distinct Subsequences II/README_EN.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,161 @@ A <strong>subsequence</strong> of a string is a new string that is formed from t
4646
### **Python3**
4747

4848
```python
49+
class Solution:
50+
def distinctSubseqII(self, s: str) -> int:
51+
mod = 10**9 + 7
52+
n = len(s)
53+
dp = [[0] * 26 for _ in range(n + 1)]
54+
for i, c in enumerate(s, 1):
55+
k = ord(c) - ord('a')
56+
for j in range(26):
57+
if j == k:
58+
dp[i][j] = sum(dp[i - 1]) % mod + 1
59+
else:
60+
dp[i][j] = dp[i - 1][j]
61+
return sum(dp[-1]) % mod
62+
```
63+
64+
```python
65+
class Solution:
66+
def distinctSubseqII(self, s: str) -> int:
67+
mod = 10**9 + 7
68+
dp = [0] * 26
69+
for c in s:
70+
i = ord(c) - ord('a')
71+
dp[i] = sum(dp) % mod + 1
72+
return sum(dp) % mod
73+
```
4974

75+
```python
76+
class Solution:
77+
def distinctSubseqII(self, s: str) -> int:
78+
mod = 10**9 + 7
79+
dp = [0] * 26
80+
ans = 0
81+
for c in s:
82+
i = ord(c) - ord('a')
83+
add = ans - dp[i] + 1
84+
ans = (ans + add) % mod
85+
dp[i] += add
86+
return ans
5087
```
5188

5289
### **Java**
5390

5491
```java
92+
class Solution {
93+
private static final int MOD = (int) 1e9 + 7;
94+
95+
public int distinctSubseqII(String s) {
96+
int[] dp = new int[26];
97+
for (int i = 0; i < s.length(); ++i) {
98+
int j = s.charAt(i) - 'a';
99+
dp[j] = sum(dp) + 1;
100+
}
101+
return sum(dp);
102+
}
103+
104+
private int sum(int[] arr) {
105+
int x = 0;
106+
for (int v : arr) {
107+
x = (x + v) % MOD;
108+
}
109+
return x;
110+
}
111+
}
112+
```
113+
114+
```java
115+
class Solution {
116+
private static final int MOD = (int) 1e9 + 7;
117+
118+
public int distinctSubseqII(String s) {
119+
int[] dp = new int[26];
120+
int ans = 0;
121+
for (int i = 0; i < s.length(); ++i) {
122+
int j = s.charAt(i) - 'a';
123+
int add = (ans - dp[j] + 1) % MOD;
124+
ans = (ans + add) % MOD;
125+
dp[j] = (dp[j] + add) % MOD;
126+
}
127+
return (ans + MOD) % MOD;
128+
}
129+
}
130+
```
131+
132+
### **C++**
133+
134+
```cpp
135+
class Solution {
136+
public:
137+
const int mod = 1e9 + 7;
138+
139+
int distinctSubseqII(string s) {
140+
vector<long> dp(26);
141+
for (char& c : s) {
142+
int i = c - 'a';
143+
dp[i] = accumulate(dp.begin(), dp.end(), 1l) % mod;
144+
}
145+
return accumulate(dp.begin(), dp.end(), 0l) % mod;
146+
}
147+
};
148+
```
149+
150+
```cpp
151+
class Solution {
152+
public:
153+
const int mod = 1e9 + 7;
154+
155+
int distinctSubseqII(string s) {
156+
vector<long> dp(26);
157+
long ans = 0;
158+
for (char& c : s) {
159+
int i = c - 'a';
160+
long add = ans - dp[i] + 1;
161+
ans = (ans + add + mod) % mod;
162+
dp[i] = (dp[i] + add) % mod;
163+
}
164+
return ans;
165+
}
166+
};
167+
```
168+
169+
### **Go**
170+
171+
```go
172+
func distinctSubseqII(s string) int {
173+
const mod int = 1e9 + 7
174+
sum := func(arr []int) int {
175+
x := 0
176+
for _, v := range arr {
177+
x = (x + v) % mod
178+
}
179+
return x
180+
}
181+
182+
dp := make([]int, 26)
183+
for _, c := range s {
184+
c -= 'a'
185+
dp[c] = sum(dp) + 1
186+
}
187+
return sum(dp)
188+
}
189+
```
55190

191+
```go
192+
func distinctSubseqII(s string) int {
193+
const mod int = 1e9 + 7
194+
dp := make([]int, 26)
195+
ans := 0
196+
for _, c := range s {
197+
c -= 'a'
198+
add := ans - dp[c] + 1
199+
ans = (ans + add) % mod
200+
dp[c] = (dp[c] + add) % mod
201+
}
202+
return (ans + mod) % mod
203+
}
56204
```
57205

58206
### **...**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
const int mod = 1e9 + 7;
4+
5+
int distinctSubseqII(string s) {
6+
vector<long> dp(26);
7+
long ans = 0;
8+
for (char& c : s) {
9+
int i = c - 'a';
10+
long add = ans - dp[i] + 1;
11+
ans = (ans + add + mod) % mod;
12+
dp[i] = (dp[i] + add) % mod;
13+
}
14+
return ans;
15+
}
16+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func distinctSubseqII(s string) int {
2+
const mod int = 1e9 + 7
3+
dp := make([]int, 26)
4+
ans := 0
5+
for _, c := range s {
6+
c -= 'a'
7+
add := ans - dp[c] + 1
8+
ans = (ans + add) % mod
9+
dp[c] = (dp[c] + add) % mod
10+
}
11+
return (ans + mod) % mod
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
private static final int MOD = (int) 1e9 + 7;
3+
4+
public int distinctSubseqII(String s) {
5+
int[] dp = new int[26];
6+
int ans = 0;
7+
for (int i = 0; i < s.length(); ++i) {
8+
int j = s.charAt(i) - 'a';
9+
int add = (ans - dp[j] + 1) % MOD;
10+
ans = (ans + add) % MOD;
11+
dp[j] = (dp[j] + add) % MOD;
12+
}
13+
return (ans + MOD) % MOD;
14+
}
15+
}

0 commit comments

Comments
 (0)