Skip to content

Commit 014e437

Browse files
committed
Add solution 0792
1 parent 578c85e commit 014e437

25 files changed

+1408
-683
lines changed

README.md

Lines changed: 434 additions & 433 deletions
Large diffs are not rendered by default.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leetcode
2+
3+
func numMatchingSubseq(s string, words []string) int {
4+
hash, res := make([][]string, 26), 0
5+
for _, w := range words {
6+
hash[int(w[0]-'a')] = append(hash[int(w[0]-'a')], w)
7+
}
8+
for _, c := range s {
9+
words := hash[int(byte(c)-'a')]
10+
hash[int(byte(c)-'a')] = []string{}
11+
for _, w := range words {
12+
if len(w) == 1 {
13+
res += 1
14+
continue
15+
}
16+
hash[int(w[1]-'a')] = append(hash[int(w[1]-'a')], w[1:])
17+
}
18+
}
19+
return res
20+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question792 struct {
9+
para792
10+
ans792
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para792 struct {
16+
s string
17+
words []string
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans792 struct {
23+
one int
24+
}
25+
26+
func Test_Problem792(t *testing.T) {
27+
28+
qs := []question792{
29+
30+
{
31+
para792{"abcde", []string{"a", "bb", "acd", "ace"}},
32+
ans792{3},
33+
},
34+
}
35+
36+
fmt.Printf("------------------------Leetcode Problem 792------------------------\n")
37+
38+
for _, q := range qs {
39+
_, p := q.ans792, q.para792
40+
fmt.Printf("【input】:%v 【output】:%v\n", p, numMatchingSubseq(p.s, p.words))
41+
}
42+
fmt.Printf("\n\n\n")
43+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [792. Number of Matching Subsequences](https://leetcode.com/problems/number-of-matching-subsequences/)
2+
3+
4+
## 题目
5+
6+
Given a string `s` and an array of strings `words`, return *the number of* `words[i]` *that is a subsequence of* `s`.
7+
8+
**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.
9+
10+
- For example, `"ace"` is a subsequence of `"abcde"`.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: s = "abcde", words = ["a","bb","acd","ace"]
16+
Output: 3
17+
Explanation: There are three strings in words that are a subsequence of s: "a", "acd", "ace".
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
24+
Output: 2
25+
```
26+
27+
**Constraints:**
28+
29+
- `1 <= s.length <= 5 * 104`
30+
- `1 <= words.length <= 5000`
31+
- `1 <= words[i].length <= 50`
32+
- `s` and `words[i]` consist of only lowercase English letters.
33+
34+
## 题目大意
35+
36+
给定字符串 S 和单词字典 words, 求 words[i] 中是 S 的子序列的单词个数。
37+
38+
## 解题思路
39+
40+
- 如果将 words 数组内的字符串每次都在源字符串 S 中匹配,这种暴力解法超时。超时原因是对字符串 S 遍历了多次。是否有更加高效的方法呢?
41+
- 把 words 数组内字符串按照首字母,分到 26 个桶中。从头开始遍历一遍源字符串 S,每扫一个字母,命中 26 个桶中其中一个桶,修改这个桶中的字符串。例如:当前遍历到了 'o',此时桶中存的数据是 'a' : ['amy','aop'], 'o': ['oqp','onwn'],那么调整 'o' 桶中的数据后,各桶的状态为,'a' : ['amy','aop'], 'q': ['qp'], 'n': ['nwn']。从头到尾扫完整个字符串 S,某个桶中的字符串被清空,说明该桶中的字符串都符合 S 的子序列。将符合子序列的字符串个数累加起来即为最终答案。
42+
43+
## 代码
44+
45+
```go
46+
package leetcode
47+
48+
func numMatchingSubseq(s string, words []string) int {
49+
hash, res := make([][]string, 26), 0
50+
for _, w := range words {
51+
hash[int(w[0]-'a')] = append(hash[int(w[0]-'a')], w)
52+
}
53+
for _, c := range s {
54+
words := hash[int(byte(c)-'a')]
55+
hash[int(byte(c)-'a')] = []string{}
56+
for _, w := range words {
57+
if len(w) == 1 {
58+
res += 1
59+
continue
60+
}
61+
hash[int(w[1]-'a')] = append(hash[int(w[1]-'a')], w[1:])
62+
}
63+
}
64+
return res
65+
}
66+
```

website/content/ChapterFour/0700~0799/0786.K-th-Smallest-Prime-Fraction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,5 @@ func (a SortByFraction) Less(i, j int) bool {
122122
----------------------------------------------
123123
<div style="display: flex;justify-content: space-between;align-items: center;">
124124
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0785.Is-Graph-Bipartite/">⬅️上一页</a></p>
125-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function/">下一页➡️</a></p>
125+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0792.Number-of-Matching-Subsequences/">下一页➡️</a></p>
126126
</div>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# [792. Number of Matching Subsequences](https://leetcode.com/problems/number-of-matching-subsequences/)
2+
3+
4+
## 题目
5+
6+
Given a string `s` and an array of strings `words`, return *the number of* `words[i]` *that is a subsequence of* `s`.
7+
8+
**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.
9+
10+
- For example, `"ace"` is a subsequence of `"abcde"`.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: s = "abcde", words = ["a","bb","acd","ace"]
16+
Output: 3
17+
Explanation: There are three strings in words that are a subsequence of s: "a", "acd", "ace".
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
24+
Output: 2
25+
```
26+
27+
**Constraints:**
28+
29+
- `1 <= s.length <= 5 * 104`
30+
- `1 <= words.length <= 5000`
31+
- `1 <= words[i].length <= 50`
32+
- `s` and `words[i]` consist of only lowercase English letters.
33+
34+
## 题目大意
35+
36+
给定字符串 S 和单词字典 words, 求 words[i] 中是 S 的子序列的单词个数。
37+
38+
## 解题思路
39+
40+
- 如果将 words 数组内的字符串每次都在源字符串 S 中匹配,这种暴力解法超时。超时原因是对字符串 S 遍历了多次。是否有更加高效的方法呢?
41+
- 把 words 数组内字符串按照首字母,分到 26 个桶中。从头开始遍历一遍源字符串 S,每扫一个字母,命中 26 个桶中其中一个桶,修改这个桶中的字符串。例如:当前遍历到了 'o',此时桶中存的数据是 'a' : ['amy','aop'], 'o': ['oqp','onwn'],那么调整 'o' 桶中的数据后,各桶的状态为,'a' : ['amy','aop'], 'q': ['qp'], 'n': ['nwn']。从头到尾扫完整个字符串 S,某个桶中的字符串被清空,说明该桶中的字符串都符合 S 的子序列。将符合子序列的字符串个数累加起来即为最终答案。
42+
43+
## 代码
44+
45+
```go
46+
package leetcode
47+
48+
func numMatchingSubseq(s string, words []string) int {
49+
hash, res := make([][]string, 26), 0
50+
for _, w := range words {
51+
hash[int(w[0]-'a')] = append(hash[int(w[0]-'a')], w)
52+
}
53+
for _, c := range s {
54+
words := hash[int(byte(c)-'a')]
55+
hash[int(byte(c)-'a')] = []string{}
56+
for _, w := range words {
57+
if len(w) == 1 {
58+
res += 1
59+
continue
60+
}
61+
hash[int(w[1]-'a')] = append(hash[int(w[1]-'a')], w[1:])
62+
}
63+
}
64+
return res
65+
}
66+
```
67+
68+
69+
----------------------------------------------
70+
<div style="display: flex;justify-content: space-between;align-items: center;">
71+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0786.K-th-Smallest-Prime-Fraction/">⬅️上一页</a></p>
72+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function/">下一页➡️</a></p>
73+
</div>

website/content/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,6 @@ func preimageSizeFZF1(K int) int {
103103

104104
----------------------------------------------
105105
<div style="display: flex;justify-content: space-between;align-items: center;">
106-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0786.K-th-Smallest-Prime-Fraction/">⬅️上一页</a></p>
106+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0792.Number-of-Matching-Subsequences/">⬅️上一页</a></p>
107107
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0795.Number-of-Subarrays-with-Bounded-Maximum/">下一页➡️</a></p>
108108
</div>

0 commit comments

Comments
 (0)