Skip to content

Commit 2caad0d

Browse files
authored
feat: add solutions to lc problem: No.1987 (#1491)
No.1987.Number of Unique Good Subsequences
1 parent 40f173e commit 2caad0d

File tree

8 files changed

+289
-2
lines changed

8 files changed

+289
-2
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171

7272
时间复杂度 $O(n)$,空间复杂度 $O(C)$。
7373

74+
相似题目:
75+
76+
- [1987. 不同的好子序列数目](/solution/1900-1999/1987.Number%20of%20Unique%20Good%20Subsequences/README.md)
77+
7478
<!-- tabs:start -->
7579

7680
### **Python3**

solution/1900-1999/1987.Number of Unique Good Subsequences/README.md

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,132 @@
5656

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

59+
**方法一:动态规划**
60+
61+
我们定义 $f$ 表示以 $1$ 结尾的不同好子序列的数目,定义 $g$ 表示以 $0$ 结尾的且以 $1$ 开头的不同好子序列的数目。初始时 $f = g = 0$。
62+
63+
对于一个二进制字符串,我们可以从左到右遍历每一位,假设当前位为 $c$,那么:
64+
65+
- 如果 $c = 0$,那么我们可以在 $f$ 和 $g$ 个不同的好子序列拼上 $c$,因此更新 $g = (g + f) \bmod (10^9 + 7)$;
66+
- 如果 $c = 1$,那么我们可以在 $f$ 和 $g$ 个不同的好子序列拼上 $c$,同时还可以单独拼上 $c$,因此更新 $f = (f + g + 1) \bmod (10^9 + 7)$。
67+
68+
如果字符串包含 $0$,那么最终答案为 $f + g + 1$,否则答案为 $f + g$。
69+
70+
时间复杂度 $O(n)$,其中 $n$ 是字符串长度。空间复杂度 $O(1)$。
71+
72+
相似题目:
73+
74+
- [940. 不同的子序列 II](/solution/0900-0999/0940.Distinct%20Subsequences%20II/README.md)
75+
5976
<!-- tabs:start -->
6077

6178
### **Python3**
6279

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

6582
```python
66-
83+
class Solution:
84+
def numberOfUniqueGoodSubsequences(self, binary: str) -> int:
85+
f = g = 0
86+
ans = 0
87+
mod = 10**9 + 7
88+
for c in binary:
89+
if c == "0":
90+
g = (g + f) % mod
91+
ans = 1
92+
else:
93+
f = (f + g + 1) % mod
94+
ans = (ans + f + g) % mod
95+
return ans
6796
```
6897

6998
### **Java**
7099

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

73102
```java
103+
class Solution {
104+
public int numberOfUniqueGoodSubsequences(String binary) {
105+
final int mod = (int) 1e9 + 7;
106+
int f = 0, g = 0;
107+
int ans = 0;
108+
for (int i = 0; i < binary.length(); ++i) {
109+
if (binary.charAt(i) == '0') {
110+
g = (g + f) % mod;
111+
ans = 1;
112+
} else {
113+
f = (f + g + 1) % mod;
114+
}
115+
}
116+
ans = (ans + f) % mod;
117+
ans = (ans + g) % mod;
118+
return ans;
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
int numberOfUniqueGoodSubsequences(string binary) {
129+
const int mod = 1e9 + 7;
130+
int f = 0, g = 0;
131+
int ans = 0;
132+
for (char& c : binary) {
133+
if (c == '0') {
134+
g = (g + f) % mod;
135+
ans = 1;
136+
} else {
137+
f = (f + g + 1) % mod;
138+
}
139+
}
140+
ans = (ans + f) % mod;
141+
ans = (ans + g) % mod;
142+
return ans;
143+
}
144+
};
145+
```
146+
147+
### **Go**
148+
149+
```go
150+
func numberOfUniqueGoodSubsequences(binary string) (ans int) {
151+
const mod int = 1e9 + 7
152+
f, g := 0, 0
153+
for _, c := range binary {
154+
if c == '0' {
155+
g = (g + f) % mod
156+
ans = 1
157+
} else {
158+
f = (f + g + 1) % mod
159+
}
160+
}
161+
ans = (ans + f + g) % mod
162+
return
163+
}
164+
```
74165

166+
### **TypeScript**
167+
168+
```ts
169+
function numberOfUniqueGoodSubsequences(binary: string): number {
170+
let [f, g] = [0, 0];
171+
let ans = 0;
172+
const mod = 1e9 + 7;
173+
for (const c of binary) {
174+
if (c === '0') {
175+
g = (g + f) % mod;
176+
ans = 1;
177+
} else {
178+
f = (f + g + 1) % mod;
179+
}
180+
}
181+
ans = (ans + f) % mod;
182+
ans = (ans + g) % mod;
183+
return ans;
184+
}
75185
```
76186

77187
### **...**

solution/1900-1999/1987.Number of Unique Good Subsequences/README_EN.md

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,106 @@ The unique good subsequences are &quot;0&quot;, &quot;1&quot;, &quot;10&quot;, &
5858
### **Python3**
5959

6060
```python
61-
61+
class Solution:
62+
def numberOfUniqueGoodSubsequences(self, binary: str) -> int:
63+
f = g = 0
64+
ans = 0
65+
mod = 10**9 + 7
66+
for c in binary:
67+
if c == "0":
68+
g = (g + f) % mod
69+
ans = 1
70+
else:
71+
f = (f + g + 1) % mod
72+
ans = (ans + f + g) % mod
73+
return ans
6274
```
6375

6476
### **Java**
6577

6678
```java
79+
class Solution {
80+
public int numberOfUniqueGoodSubsequences(String binary) {
81+
final int mod = (int) 1e9 + 7;
82+
int f = 0, g = 0;
83+
int ans = 0;
84+
for (int i = 0; i < binary.length(); ++i) {
85+
if (binary.charAt(i) == '0') {
86+
g = (g + f) % mod;
87+
ans = 1;
88+
} else {
89+
f = (f + g + 1) % mod;
90+
}
91+
}
92+
ans = (ans + f) % mod;
93+
ans = (ans + g) % mod;
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int numberOfUniqueGoodSubsequences(string binary) {
105+
const int mod = 1e9 + 7;
106+
int f = 0, g = 0;
107+
int ans = 0;
108+
for (char& c : binary) {
109+
if (c == '0') {
110+
g = (g + f) % mod;
111+
ans = 1;
112+
} else {
113+
f = (f + g + 1) % mod;
114+
}
115+
}
116+
ans = (ans + f) % mod;
117+
ans = (ans + g) % mod;
118+
return ans;
119+
}
120+
};
121+
```
122+
123+
### **Go**
124+
125+
```go
126+
func numberOfUniqueGoodSubsequences(binary string) (ans int) {
127+
const mod int = 1e9 + 7
128+
f, g := 0, 0
129+
for _, c := range binary {
130+
if c == '0' {
131+
g = (g + f) % mod
132+
ans = 1
133+
} else {
134+
f = (f + g + 1) % mod
135+
}
136+
}
137+
ans = (ans + f + g) % mod
138+
return
139+
}
140+
```
67141

142+
### **TypeScript**
143+
144+
```ts
145+
function numberOfUniqueGoodSubsequences(binary: string): number {
146+
let [f, g] = [0, 0];
147+
let ans = 0;
148+
const mod = 1e9 + 7;
149+
for (const c of binary) {
150+
if (c === '0') {
151+
g = (g + f) % mod;
152+
ans = 1;
153+
} else {
154+
f = (f + g + 1) % mod;
155+
}
156+
}
157+
ans = (ans + f) % mod;
158+
ans = (ans + g) % mod;
159+
return ans;
160+
}
68161
```
69162

70163
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int numberOfUniqueGoodSubsequences(string binary) {
4+
const int mod = 1e9 + 7;
5+
int f = 0, g = 0;
6+
int ans = 0;
7+
for (char& c : binary) {
8+
if (c == '0') {
9+
g = (g + f) % mod;
10+
ans = 1;
11+
} else {
12+
f = (f + g + 1) % mod;
13+
}
14+
}
15+
ans = (ans + f) % mod;
16+
ans = (ans + g) % mod;
17+
return ans;
18+
}
19+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func numberOfUniqueGoodSubsequences(binary string) (ans int) {
2+
const mod int = 1e9 + 7
3+
f, g := 0, 0
4+
for _, c := range binary {
5+
if c == '0' {
6+
g = (g + f) % mod
7+
ans = 1
8+
} else {
9+
f = (f + g + 1) % mod
10+
}
11+
}
12+
ans = (ans + f + g) % mod
13+
return
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int numberOfUniqueGoodSubsequences(String binary) {
3+
final int mod = (int) 1e9 + 7;
4+
int f = 0, g = 0;
5+
int ans = 0;
6+
for (int i = 0; i < binary.length(); ++i) {
7+
if (binary.charAt(i) == '0') {
8+
g = (g + f) % mod;
9+
ans = 1;
10+
} else {
11+
f = (f + g + 1) % mod;
12+
}
13+
}
14+
ans = (ans + f) % mod;
15+
ans = (ans + g) % mod;
16+
return ans;
17+
}
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def numberOfUniqueGoodSubsequences(self, binary: str) -> int:
3+
f = g = 0
4+
ans = 0
5+
mod = 10**9 + 7
6+
for c in binary:
7+
if c == "0":
8+
g = (g + f) % mod
9+
ans = 1
10+
else:
11+
f = (f + g + 1) % mod
12+
ans = (ans + f + g) % mod
13+
return ans
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function numberOfUniqueGoodSubsequences(binary: string): number {
2+
let [f, g] = [0, 0];
3+
let ans = 0;
4+
const mod = 1e9 + 7;
5+
for (const c of binary) {
6+
if (c === '0') {
7+
g = (g + f) % mod;
8+
ans = 1;
9+
} else {
10+
f = (f + g + 1) % mod;
11+
}
12+
}
13+
ans = (ans + f) % mod;
14+
ans = (ans + g) % mod;
15+
return ans;
16+
}

0 commit comments

Comments
 (0)