Skip to content

Commit 2971ce4

Browse files
committed
+ problem 940
1 parent 61d2dc9 commit 2971ce4

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 940. Distinct Subsequences II
2+
Given a string s, return *the number of **distinct non-empty subsequences** of* `s`. Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
3+
4+
A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., `"ace"` is a subsequence of `"abcde"` while `"aec"` is not.
5+
6+
#### Example 1:
7+
<pre>
8+
<strong>Input:</strong> s = "abc"
9+
<strong>Output:</strong> 7
10+
<strong>Explanation:</strong> The 7 distinct subsequences are "a", "b", "c", "ab", "ac", "bc", and "abc".
11+
</pre>
12+
13+
#### Example 2:
14+
<pre>
15+
<strong>Input:</strong> s = "aba"
16+
<strong>Output:</strong> 6
17+
<strong>Explanation:</strong> The 6 distinct subsequences are "a", "b", "ab", "aa", "ba", and "aba".
18+
</pre>
19+
20+
#### Example 3:
21+
<pre>
22+
<strong>Input:</strong> s = "aaa"
23+
<strong>Output:</strong> 3
24+
<strong>Explanation:</strong> The 3 distinct subsequences are "a", "aa" and "aaa".
25+
</pre>
26+
27+
#### Constraints:
28+
* `1 <= s.length <= 2000`
29+
* `s` consists of lowercase English letters.
30+
31+
## Solutions (Rust)
32+
33+
### 1. Solution
34+
```Rust
35+
impl Solution {
36+
pub fn distinct_subseq_ii(s: String) -> i32 {
37+
const MOD: i32 = 1_000_000_007;
38+
let n = s.len();
39+
let mut last_index = [n; 26];
40+
let mut dp = vec![0_i32; n + 1];
41+
dp[n] = -1;
42+
43+
for (i, c) in s.bytes().map(|c| (c - b'a') as usize).enumerate() {
44+
dp[i + 1] = (dp[i] * 2 - dp[last_index[c]]).rem_euclid(MOD);
45+
last_index[c] = i;
46+
}
47+
48+
*dp.last().unwrap()
49+
}
50+
}
51+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 940. 不同的子序列 II
2+
给定一个字符串 `s`,计算 `s`**不同非空子序列** 的个数。因为结果可能很大,所以返回答案需要对 `10^9 + 7` **取余**
3+
4+
字符串的 **子序列** 是经由原字符串删除一些(也可能不删除)字符但不改变剩余字符相对位置的一个新字符串。
5+
6+
* 例如,`"ace"``"abcde"` 的一个子序列,但 `"aec"` 不是。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> s = "abc"
11+
<strong>输出:</strong> 7
12+
<strong>解释:</strong> 7 个不同的子序列分别是 "a", "b", "c", "ab", "ac", "bc", 以及 "abc"。
13+
</pre>
14+
15+
#### 示例 2:
16+
<pre>
17+
<strong>输入:</strong> s = "aba"
18+
<strong>输出:</strong> 6
19+
<strong>解释:</strong> 6 个不同的子序列分别是 "a", "b", "ab", "ba", "aa" 以及 "aba"。
20+
</pre>
21+
22+
#### 示例 3:
23+
<pre>
24+
<strong>输入:</strong> s = "aaa"
25+
<strong>输出:</strong> 3
26+
<strong>解释:</strong> 3 个不同的子序列分别是 "a", "aa" 以及 "aaa"。
27+
</pre>
28+
29+
#### 提示:
30+
* `1 <= s.length <= 2000`
31+
* `s` 仅由小写英文字母组成
32+
33+
## 题解 (Rust)
34+
35+
### 1. 题解
36+
```Rust
37+
impl Solution {
38+
pub fn distinct_subseq_ii(s: String) -> i32 {
39+
const MOD: i32 = 1_000_000_007;
40+
let n = s.len();
41+
let mut last_index = [n; 26];
42+
let mut dp = vec![0_i32; n + 1];
43+
dp[n] = -1;
44+
45+
for (i, c) in s.bytes().map(|c| (c - b'a') as usize).enumerate() {
46+
dp[i + 1] = (dp[i] * 2 - dp[last_index[c]]).rem_euclid(MOD);
47+
last_index[c] = i;
48+
}
49+
50+
*dp.last().unwrap()
51+
}
52+
}
53+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn distinct_subseq_ii(s: String) -> i32 {
3+
const MOD: i32 = 1_000_000_007;
4+
let n = s.len();
5+
let mut last_index = [n; 26];
6+
let mut dp = vec![0_i32; n + 1];
7+
dp[n] = -1;
8+
9+
for (i, c) in s.bytes().map(|c| (c - b'a') as usize).enumerate() {
10+
dp[i + 1] = (dp[i] * 2 - dp[last_index[c]]).rem_euclid(MOD);
11+
last_index[c] = i;
12+
}
13+
14+
*dp.last().unwrap()
15+
}
16+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@
714714
[937][937l] |[Reorder Data in Log Files][937] |![py]
715715
[938][938l] |[Range Sum of BST][938] |![py]
716716
[939][939l] |[Minimum Area Rectangle][939] |![py]&nbsp;&nbsp;![rs]
717+
[940][940l] |[Distinct Subsequences II][940] |![rs]
717718
[941][941l] |[Valid Mountain Array][941] |![rs]
718719
[942][942l] |[DI String Match][942] |![py]
719720
[944][944l] |[Delete Columns to Make Sorted][944] |![py]
@@ -2645,6 +2646,7 @@
26452646
[937]:Problemset/0937-Reorder%20Data%20in%20Log%20Files/README.md#937-reorder-data-in-log-files
26462647
[938]:Problemset/0938-Range%20Sum%20of%20BST/README.md#938-range-sum-of-bst
26472648
[939]:Problemset/0939-Minimum%20Area%20Rectangle/README.md#939-minimum-area-rectangle
2649+
[940]:Problemset/0940-Distinct%20Subsequences%20II/README.md#940-distinct-subsequences-ii
26482650
[941]:Problemset/0941-Valid%20Mountain%20Array/README.md#941-valid-mountain-array
26492651
[942]:Problemset/0942-DI%20String%20Match/README.md#942-di-string-match
26502652
[944]:Problemset/0944-Delete%20Columns%20to%20Make%20Sorted/README.md#944-delete-columns-to-make-sorted
@@ -4570,6 +4572,7 @@
45704572
[937l]:https://leetcode.com/problems/reorder-data-in-log-files/
45714573
[938l]:https://leetcode.com/problems/range-sum-of-bst/
45724574
[939l]:https://leetcode.com/problems/minimum-area-rectangle/
4575+
[940l]:https://leetcode.com/problems/distinct-subsequences-ii/
45734576
[941l]:https://leetcode.com/problems/valid-mountain-array/
45744577
[942l]:https://leetcode.com/problems/di-string-match/
45754578
[944l]:https://leetcode.com/problems/delete-columns-to-make-sorted/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@
714714
[937][937l] |[重新排列日志文件][937] |![py]
715715
[938][938l] |[二叉搜索树的范围和][938] |![py]
716716
[939][939l] |[最小面积矩形][939] |![py]&nbsp;&nbsp;![rs]
717+
[940][940l] |[不同的子序列 II][940] |![rs]
717718
[941][941l] |[有效的山脉数组][941] |![rs]
718719
[942][942l] |[增减字符串匹配][942] |![py]
719720
[944][944l] |[删列造序][944] |![py]
@@ -2645,6 +2646,7 @@
26452646
[937]:Problemset/0937-Reorder%20Data%20in%20Log%20Files/README_CN.md#937-重新排列日志文件
26462647
[938]:Problemset/0938-Range%20Sum%20of%20BST/README_CN.md#938-二叉搜索树的范围和
26472648
[939]:Problemset/0939-Minimum%20Area%20Rectangle/README_CN.md#939-最小面积矩形
2649+
[940]:Problemset/0940-Distinct%20Subsequences%20II/README_CN.md#940-不同的子序列-ii
26482650
[941]:Problemset/0941-Valid%20Mountain%20Array/README_CN.md#941-有效的山脉数组
26492651
[942]:Problemset/0942-DI%20String%20Match/README_CN.md#942-增减字符串匹配
26502652
[944]:Problemset/0944-Delete%20Columns%20to%20Make%20Sorted/README_CN.md#944-删列造序
@@ -4570,6 +4572,7 @@
45704572
[937l]:https://leetcode.cn/problems/reorder-data-in-log-files/
45714573
[938l]:https://leetcode.cn/problems/range-sum-of-bst/
45724574
[939l]:https://leetcode.cn/problems/minimum-area-rectangle/
4575+
[940l]:https://leetcode.cn/problems/distinct-subsequences-ii/
45734576
[941l]:https://leetcode.cn/problems/valid-mountain-array/
45744577
[942l]:https://leetcode.cn/problems/di-string-match/
45754578
[944l]:https://leetcode.cn/problems/delete-columns-to-make-sorted/

0 commit comments

Comments
 (0)