Skip to content

Commit 40c4709

Browse files
committed
https://leetcode.cn/problems/number-of-matching-subsequences
1 parent cdec29b commit 40c4709

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Step 2. Add the dependency
4545

4646
<summary>展开查看</summary>
4747

48+
https://leetcode.cn/problems/number-of-matching-subsequences
49+
4850
https://leetcode.cn/problems/global-and-local-inversions
4951

5052
https://leetcode.cn/problems/maximum-units-on-a-truck
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { counter } from "../substring-with-concatenation-of-all-words/counter.ts";
2+
3+
function numMatchingSubseq(s: string, words: string[]): number {
4+
const cnt = counter(words);
5+
const arr = Array(26)
6+
.fill(0)
7+
.map(() => new Set<[string, number]>());
8+
9+
for (const [w, count] of cnt) {
10+
arr[w.charCodeAt(0) - "a".charCodeAt(0)].add([w, count]);
11+
}
12+
let res = 0;
13+
for (const c of s) {
14+
const temp = arr[c.charCodeAt(0) - "a".charCodeAt(0)];
15+
for (const wordcount of temp) {
16+
const [word, count1] = wordcount;
17+
if (word.length === 1) {
18+
res += count1;
19+
temp.delete(wordcount);
20+
} else {
21+
const w1 = word.slice(1);
22+
if (w1.charCodeAt(0) === word.charCodeAt(0)) {
23+
wordcount[0] = word.slice(1);
24+
} else {
25+
temp.delete(wordcount);
26+
arr[w1.charCodeAt(0) - "a".charCodeAt(0)].add([w1, count1]);
27+
}
28+
}
29+
}
30+
}
31+
32+
return res;
33+
}
34+
export default numMatchingSubseq;

0 commit comments

Comments
 (0)