Skip to content

Commit a006f41

Browse files
committed
+ problem 792
1 parent 98b4e9c commit a006f41

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 792. Number of Matching Subsequences
2+
Given a string `s` and an array of strings `words`, return *the number of* `words[i]` *that is a subsequence of* `s`.
3+
4+
A **subsequence** of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
5+
6+
* For example, `"ace"` is a subsequence of `"abcde"`.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> s = "abcde", words = ["a","bb","acd","ace"]
11+
<strong>Output:</strong> 3
12+
<strong>Explanation:</strong> There are three strings in words that are a subsequence of s: "a", "acd", "ace".
13+
</pre>
14+
15+
#### Example 2:
16+
<pre>
17+
<strong>Input:</strong> s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
18+
<strong>Output:</strong> 2
19+
</pre>
20+
21+
#### Constraints:
22+
* <code>1 <= s.length <= 5 * 10<sup>4</sup></code>
23+
* `1 <= words.length <= 5000`
24+
* `1 <= words[i].length <= 50`
25+
* `s` and `words[i]` consist of only lowercase English letters.
26+
27+
## Solutions (Rust)
28+
29+
### 1. Solution
30+
```Rust
31+
impl Solution {
32+
pub fn num_matching_subseq(s: String, words: Vec<String>) -> i32 {
33+
let mut indices = vec![vec![]; 26];
34+
let mut ret = 0;
35+
36+
for (i, c) in s.bytes().enumerate() {
37+
indices[(c - b'a') as usize].push(i);
38+
}
39+
40+
for word in &words {
41+
let mut i = 0;
42+
let mut flag = true;
43+
44+
for c in word.bytes() {
45+
match indices[(c - b'a') as usize].binary_search(&i) {
46+
Err(j) if j == indices[(c - b'a') as usize].len() => {
47+
flag = false;
48+
break;
49+
}
50+
Ok(j) | Err(j) => i = indices[(c - b'a') as usize][j] + 1,
51+
}
52+
}
53+
54+
ret += flag as i32;
55+
}
56+
57+
ret
58+
}
59+
}
60+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 792. 匹配子序列的单词数
2+
给定字符串 `s` 和字符串数组 `words`, 返回 *`words[i]` 中是`s`的子序列的单词个数*
3+
4+
字符串的 **子序列** 是从原始字符串中生成的新字符串,可以从中删去一些字符(可以是none),而不改变其余字符的相对顺序。
5+
6+
* 例如, `“ace”``“abcde”` 的子序列。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> s = "abcde", words = ["a","bb","acd","ace"]
11+
<strong>输出:</strong> 3
12+
<strong>解释:</strong> 有三个是 s 的子序列的单词: "a", "acd", "ace"。
13+
</pre>
14+
15+
#### 示例 2:
16+
<pre>
17+
<strong>输入:</strong> s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
18+
<strong>输出:</strong> 2
19+
</pre>
20+
21+
#### 提示:
22+
* <code>1 <= s.length <= 5 * 10<sup>4</sup></code>
23+
* `1 <= words.length <= 5000`
24+
* `1 <= words[i].length <= 50`
25+
* `words[i]``s` 都只由小写字母组成。
26+
* `s` and `words[i]` consist of only lowercase English letters.
27+
28+
## 题解 (Rust)
29+
30+
### 1. 题解
31+
```Rust
32+
impl Solution {
33+
pub fn num_matching_subseq(s: String, words: Vec<String>) -> i32 {
34+
let mut indices = vec![vec![]; 26];
35+
let mut ret = 0;
36+
37+
for (i, c) in s.bytes().enumerate() {
38+
indices[(c - b'a') as usize].push(i);
39+
}
40+
41+
for word in &words {
42+
let mut i = 0;
43+
let mut flag = true;
44+
45+
for c in word.bytes() {
46+
match indices[(c - b'a') as usize].binary_search(&i) {
47+
Err(j) if j == indices[(c - b'a') as usize].len() => {
48+
flag = false;
49+
break;
50+
}
51+
Ok(j) | Err(j) => i = indices[(c - b'a') as usize][j] + 1,
52+
}
53+
}
54+
55+
ret += flag as i32;
56+
}
57+
58+
ret
59+
}
60+
}
61+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
impl Solution {
2+
pub fn num_matching_subseq(s: String, words: Vec<String>) -> i32 {
3+
let mut indices = vec![vec![]; 26];
4+
let mut ret = 0;
5+
6+
for (i, c) in s.bytes().enumerate() {
7+
indices[(c - b'a') as usize].push(i);
8+
}
9+
10+
for word in &words {
11+
let mut i = 0;
12+
let mut flag = true;
13+
14+
for c in word.bytes() {
15+
match indices[(c - b'a') as usize].binary_search(&i) {
16+
Err(j) if j == indices[(c - b'a') as usize].len() => {
17+
flag = false;
18+
break;
19+
}
20+
Ok(j) | Err(j) => i = indices[(c - b'a') as usize][j] + 1,
21+
}
22+
}
23+
24+
ret += flag as i32;
25+
}
26+
27+
ret
28+
}
29+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@
486486
[789][789l] |[Escape The Ghosts][789] |![rb]&nbsp;&nbsp;![rs]
487487
[790][790l] |[Domino and Tromino Tiling][790] |![rs]
488488
[791][791l] |[Custom Sort String][791] |![rs]
489+
[792][792l] |[Number of Matching Subsequences][792] |![rs]
489490
[794][794l] |[Valid Tic-Tac-Toe State][794] |![rs]
490491
[796][796l] |[Rotate String][796] |![rs]
491492
[798][798l] |[Smallest Rotation with Highest Score][798] |![rs]
@@ -1967,6 +1968,7 @@
19671968
[789]:Problemset/0789-Escape%20The%20Ghosts/README.md#789-escape-the-ghosts
19681969
[790]:Problemset/0790-Domino%20and%20Tromino%20Tiling/README.md#790-domino-and-tromino-tiling
19691970
[791]:Problemset/0791-Custom%20Sort%20String/README.md#791-custom-sort-string
1971+
[792]:Problemset/0792-Number%20of%20Matching%20Subsequences/README.md#792-number-of-matching-subsequences
19701972
[794]:Problemset/0794-Valid%20Tic-Tac-Toe%20State/README.md#794-valid-tic-tac-toe-state
19711973
[796]:Problemset/0796-Rotate%20String/README.md#796-rotate-string
19721974
[798]:Problemset/0798-Smallest%20Rotation%20with%20Highest%20Score/README.md#798-smallest-rotation-with-highest-score
@@ -3447,6 +3449,7 @@
34473449
[789l]:https://leetcode.com/problems/escape-the-ghosts/
34483450
[790l]:https://leetcode.com/problems/domino-and-tromino-tiling/
34493451
[791l]:https://leetcode.com/problems/custom-sort-string/
3452+
[792l]:https://leetcode.com/problems/number-of-matching-subsequences/
34503453
[794l]:https://leetcode.com/problems/valid-tic-tac-toe-state/
34513454
[796l]:https://leetcode.com/problems/rotate-string/
34523455
[798l]:https://leetcode.com/problems/smallest-rotation-with-highest-score/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@
486486
[789][789l] |[逃脱阻碍者][789] |![rb]&nbsp;&nbsp;![rs]
487487
[790][790l] |[多米诺和托米诺平铺][790] |![rs]
488488
[791][791l] |[自定义字符串排序][791] |![rs]
489+
[792][792l] |[匹配子序列的单词数][792] |![rs]
489490
[794][794l] |[有效的井字游戏][794] |![rs]
490491
[796][796l] |[旋转字符串][796] |![rs]
491492
[798][798l] |[得分最高的最小轮调][798] |![rs]
@@ -1967,6 +1968,7 @@
19671968
[789]:Problemset/0789-Escape%20The%20Ghosts/README_CN.md#789-逃脱阻碍者
19681969
[790]:Problemset/0790-Domino%20and%20Tromino%20Tiling/README_CN.md#790-多米诺和托米诺平铺
19691970
[791]:Problemset/0791-Custom%20Sort%20String/README_CN.md#791-自定义字符串排序
1971+
[792]:Problemset/0792-Number%20of%20Matching%20Subsequences/README_CN.md#792-匹配子序列的单词数
19701972
[794]:Problemset/0794-Valid%20Tic-Tac-Toe%20State/README_CN.md#794-有效的井字游戏
19711973
[796]:Problemset/0796-Rotate%20String/README_CN.md#796-旋转字符串
19721974
[798]:Problemset/0798-Smallest%20Rotation%20with%20Highest%20Score/README_CN.md#798-得分最高的最小轮调
@@ -3447,6 +3449,7 @@
34473449
[789l]:https://leetcode.cn/problems/escape-the-ghosts/
34483450
[790l]:https://leetcode.cn/problems/domino-and-tromino-tiling/
34493451
[791l]:https://leetcode.cn/problems/custom-sort-string/
3452+
[792l]:https://leetcode.cn/problems/number-of-matching-subsequences/
34503453
[794l]:https://leetcode.cn/problems/valid-tic-tac-toe-state/
34513454
[796l]:https://leetcode.cn/problems/rotate-string/
34523455
[798l]:https://leetcode.cn/problems/smallest-rotation-with-highest-score/

0 commit comments

Comments
 (0)