Skip to content

Commit bf431b2

Browse files
committed
feat: add solutions to lc problem: No.0792.Number of Matching Subsequences
1 parent b836cee commit bf431b2

File tree

9 files changed

+273
-16
lines changed

9 files changed

+273
-16
lines changed

solution/0300-0399/0392.Is Subsequence/README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,9 @@ public:
112112
bool isSubsequence(string s, string t) {
113113
int m = s.size(), n = t.size();
114114
int i = 0, j = 0;
115-
while (i < m && j < n) {
116-
if (s[i] == t[j]) {
117-
++i;
118-
}
115+
while (i < m && j < n)
116+
{
117+
if (s[i] == t[j]) ++i;
119118
++j;
120119
}
121120
return i == m;

solution/0300-0399/0392.Is Subsequence/README_EN.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ public:
8787
bool isSubsequence(string s, string t) {
8888
int m = s.size(), n = t.size();
8989
int i = 0, j = 0;
90-
while (i < m && j < n) {
91-
if (s[i] == t[j]) {
92-
++i;
93-
}
90+
while (i < m && j < n)
91+
{
92+
if (s[i] == t[j]) ++i;
9493
++j;
9594
}
9695
return i == m;

solution/0300-0399/0392.Is Subsequence/Solution.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ class Solution {
33
bool isSubsequence(string s, string t) {
44
int m = s.size(), n = t.size();
55
int i = 0, j = 0;
6-
while (i < m && j < n) {
7-
if (s[i] == t[j]) {
8-
++i;
9-
}
6+
while (i < m && j < n)
7+
{
8+
if (s[i] == t[j]) ++i;
109
++j;
1110
}
1211
return i == m;

solution/0700-0799/0792.Number of Matching Subsequences/README.md

+101-2
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,126 @@ words = [&quot;a&quot;, &quot;bb&quot;, &quot;acd&quot;, &quot;ace&quot;]
2626
<li><code>words[i]</code>的长度在<code>[1, 50]</code>。</li>
2727
</ul>
2828

29-
3029
## 解法
3130

3231
<!-- 这里可写通用的实现逻辑 -->
3332

33+
由于字符串 S 长度较大,如果暴力求解,会报超时错误,因此要避免对 S 的多次遍历。
34+
35+
可以将所有单词根据首字母不同放入不同的 buckets 中,比如对于 `words = ["a", "bb", "acd", "ace"]`,可以初始化 buckets 为如下形式:
36+
37+
```python
38+
buckets = {
39+
'a': ["a", "acd", "ace"],
40+
'b': ["bb"],
41+
}
42+
```
43+
44+
然后遍历 S 中每个字符 c,在 buckets 中找到对应的 bucket 并取出所有元素 old。对于每个元素 t,如果长度为 1,说明 t 对应的单词已经遍历结束,该单词属于 S 的一个子序列,累加 res;否则将 t 取子串 `t[1:]` 放入 buckets 中。继续往下遍历 S,直至结束。
45+
46+
最后返回 res 即可。
47+
3448
<!-- tabs:start -->
3549

3650
### **Python3**
3751

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

4054
```python
41-
55+
class Solution:
56+
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
57+
buckets = collections.defaultdict(list)
58+
for word in words:
59+
buckets[word[0]].append(word)
60+
res = 0
61+
for c in s:
62+
old = buckets[c][::1]
63+
buckets[c].clear()
64+
for t in old:
65+
if len(t) == 1:
66+
res += 1
67+
else:
68+
buckets[t[1]].append(t[1:])
69+
return res
4270
```
4371

4472
### **Java**
4573

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

4876
```java
77+
class Solution {
78+
public int numMatchingSubseq(String s, String[] words) {
79+
List<String>[] buckets = new List[26];
80+
for (int i = 0; i < buckets.length; ++i) {
81+
buckets[i] = new ArrayList<>();
82+
}
83+
for (String word : words) {
84+
buckets[word.charAt(0) - 'a'].add(word);
85+
}
86+
int res = 0;
87+
for (char c : s.toCharArray()) {
88+
List<String> old = new ArrayList<>(buckets[c - 'a']);
89+
buckets[c - 'a'].clear();
90+
for (String t : old) {
91+
if (t.length() == 1) {
92+
++res;
93+
} else {
94+
buckets[t.charAt(1) - 'a'].add(t.substring(1));
95+
}
96+
}
97+
}
98+
return res;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int numMatchingSubseq(string s, vector<string>& words) {
109+
vector<vector<string>> buckets(26);
110+
for (auto word : words) buckets[word[0] - 'a'].push_back(word);
111+
int res = 0;
112+
for (auto c : s)
113+
{
114+
auto old = buckets[c - 'a'];
115+
buckets[c - 'a'].clear();
116+
for (auto t : old)
117+
{
118+
if (t.size() == 1) ++res;
119+
else buckets[t[1] - 'a'].push_back(t.substr(1));
120+
}
121+
}
122+
return res;
123+
}
124+
};
125+
```
49126
127+
### **Go**
128+
129+
```go
130+
func numMatchingSubseq(s string, words []string) int {
131+
buckets := make([][]string, 26)
132+
for _, word := range words {
133+
buckets[word[0]-'a'] = append(buckets[word[0]-'a'], word)
134+
}
135+
res := 0
136+
for _, c := range s {
137+
old := buckets[c-'a']
138+
buckets[c-'a'] = nil
139+
for _, t := range old {
140+
if len(t) == 1 {
141+
res++
142+
} else {
143+
buckets[t[1]-'a'] = append(buckets[t[1]-'a'], t[1:])
144+
}
145+
}
146+
}
147+
return res
148+
}
50149
```
51150

52151
### **...**

solution/0700-0799/0792.Number of Matching Subsequences/README_EN.md

+86-2
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,105 @@
3838
<li><code>s</code> and <code>words[i]</code> consist of only lowercase English letters.</li>
3939
</ul>
4040

41-
4241
## Solutions
4342

4443
<!-- tabs:start -->
4544

4645
### **Python3**
4746

4847
```python
49-
48+
class Solution:
49+
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
50+
buckets = collections.defaultdict(list)
51+
for word in words:
52+
buckets[word[0]].append(word)
53+
res = 0
54+
for c in s:
55+
old = buckets[c][::1]
56+
buckets[c].clear()
57+
for t in old:
58+
if len(t) == 1:
59+
res += 1
60+
else:
61+
buckets[t[1]].append(t[1:])
62+
return res
5063
```
5164

5265
### **Java**
5366

5467
```java
68+
class Solution {
69+
public int numMatchingSubseq(String s, String[] words) {
70+
List<String>[] buckets = new List[26];
71+
for (int i = 0; i < buckets.length; ++i) {
72+
buckets[i] = new ArrayList<>();
73+
}
74+
for (String word : words) {
75+
buckets[word.charAt(0) - 'a'].add(word);
76+
}
77+
int res = 0;
78+
for (char c : s.toCharArray()) {
79+
List<String> old = new ArrayList<>(buckets[c - 'a']);
80+
buckets[c - 'a'].clear();
81+
for (String t : old) {
82+
if (t.length() == 1) {
83+
++res;
84+
} else {
85+
buckets[t.charAt(1) - 'a'].add(t.substring(1));
86+
}
87+
}
88+
}
89+
return res;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int numMatchingSubseq(string s, vector<string>& words) {
100+
vector<vector<string>> buckets(26);
101+
for (auto word : words) buckets[word[0] - 'a'].push_back(word);
102+
int res = 0;
103+
for (auto c : s)
104+
{
105+
auto old = buckets[c - 'a'];
106+
buckets[c - 'a'].clear();
107+
for (auto t : old)
108+
{
109+
if (t.size() == 1) ++res;
110+
else buckets[t[1] - 'a'].push_back(t.substr(1));
111+
}
112+
}
113+
return res;
114+
}
115+
};
116+
```
55117
118+
### **Go**
119+
120+
```go
121+
func numMatchingSubseq(s string, words []string) int {
122+
buckets := make([][]string, 26)
123+
for _, word := range words {
124+
buckets[word[0]-'a'] = append(buckets[word[0]-'a'], word)
125+
}
126+
res := 0
127+
for _, c := range s {
128+
old := buckets[c-'a']
129+
buckets[c-'a'] = nil
130+
for _, t := range old {
131+
if len(t) == 1 {
132+
res++
133+
} else {
134+
buckets[t[1]-'a'] = append(buckets[t[1]-'a'], t[1:])
135+
}
136+
}
137+
}
138+
return res
139+
}
56140
```
57141

58142
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int numMatchingSubseq(string s, vector<string>& words) {
4+
vector<vector<string>> buckets(26);
5+
for (auto word : words) buckets[word[0] - 'a'].push_back(word);
6+
int res = 0;
7+
for (auto c : s)
8+
{
9+
auto old = buckets[c - 'a'];
10+
buckets[c - 'a'].clear();
11+
for (auto t : old)
12+
{
13+
if (t.size() == 1) ++res;
14+
else buckets[t[1] - 'a'].push_back(t.substr(1));
15+
}
16+
}
17+
return res;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func numMatchingSubseq(s string, words []string) int {
2+
buckets := make([][]string, 26)
3+
for _, word := range words {
4+
buckets[word[0]-'a'] = append(buckets[word[0]-'a'], word)
5+
}
6+
res := 0
7+
for _, c := range s {
8+
old := buckets[c-'a']
9+
buckets[c-'a'] = nil
10+
for _, t := range old {
11+
if len(t) == 1 {
12+
res++
13+
} else {
14+
buckets[t[1]-'a'] = append(buckets[t[1]-'a'], t[1:])
15+
}
16+
}
17+
}
18+
return res
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int numMatchingSubseq(String s, String[] words) {
3+
List<String>[] buckets = new List[26];
4+
for (int i = 0; i < buckets.length; ++i) {
5+
buckets[i] = new ArrayList<>();
6+
}
7+
for (String word : words) {
8+
buckets[word.charAt(0) - 'a'].add(word);
9+
}
10+
int res = 0;
11+
for (char c : s.toCharArray()) {
12+
List<String> old = new ArrayList<>(buckets[c - 'a']);
13+
buckets[c - 'a'].clear();
14+
for (String t : old) {
15+
if (t.length() == 1) {
16+
++res;
17+
} else {
18+
buckets[t.charAt(1) - 'a'].add(t.substring(1));
19+
}
20+
}
21+
}
22+
return res;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
3+
buckets = collections.defaultdict(list)
4+
for word in words:
5+
buckets[word[0]].append(word)
6+
res = 0
7+
for c in s:
8+
old = buckets[c][::1]
9+
buckets[c].clear()
10+
for t in old:
11+
if len(t) == 1:
12+
res += 1
13+
else:
14+
buckets[t[1]].append(t[1:])
15+
return res

0 commit comments

Comments
 (0)