-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnum_matching_subseq.cpp
More file actions
74 lines (67 loc) · 1.73 KB
/
num_matching_subseq.cpp
File metadata and controls
74 lines (67 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* =====================================================================================
*
* Filename: num_matching_subseq.cpp
*
* Description: 792. Number of Matching Subsequences
*
* Version: 1.0
* Created: 11/12/2025 11:34:05
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#include "gtest/gtest.h"
// Time complexity: O(mlogn)
// Space complexity: O(n)
class Solution {
public:
int numMatchingSubseq(std::string s, std::vector<std::string>& words) {
const int n = s.length();
std::unordered_map<char, std::set<int>> indexes;
for (int i = 0; i < n; i++) {
indexes[s[i]].insert(i);
}
int res = 0;
for (const auto& w : words) {
const int m = w.length();
if (m > n) {
continue;
}
int start = -1;
for (int i = 0; i < m; i++) {
auto it1 = indexes.find(w[i]);
if (it1 == indexes.end()) {
break;
}
auto it2 = it1->second.upper_bound(start);
if (it2 == it1->second.end()) {
break;
}
start = *it2;
if (i == m - 1) {
res++;
}
}
}
return res;
}
};
TEST(Solution, numMatchingSubseq) {
std::vector<std::tuple<std::string, std::vector<std::string>, int>> cases = {
{"abcde", {"a", "bb", "acd", "ace"}, 3},
{"dsahjpjauf", {"ahjpjau", "ja", "ahbwzgqnuk", "tnmlanowax"}, 2},
};
for (auto& [s, words, res] : cases) {
EXPECT_EQ(Solution().numMatchingSubseq(s, words), res);
}
}