forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
46 lines (36 loc) · 1.11 KB
/
main.cpp
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
/// Source : https://leetcode.com/problems/number-of-matching-subsequences/description/
/// Author : liuyubobobo
/// Time : 2018-03-05
#include <iostream>
#include <vector>
using namespace std;
/// Next Letter Pointer
/// Time Complexity:
/// Space Complexity:
class Solution {
public:
int numMatchingSubseq(string S, vector<string>& words) {
vector<pair<string, int>> table[26];
for(const string& word: words)
table[word[0] - 'a'].push_back(make_pair(word, 0));
int res = 0;
for(char c: S){
vector<pair<string, int>> old_table(table[c-'a'].begin(), table[c-'a'].end());
table[c-'a'].clear();
for(pair<string, int> p: old_table){
p.second ++;
if(p.second == p.first.size())
res ++;
else
table[p.first[p.second] - 'a'].push_back(p);
}
}
return res;
}
};
int main() {
vector<string> words = {"a", "bb", "acd", "ace"};
string S = "abcde";
cout << Solution().numMatchingSubseq(S, words) << endl;
return 0;
}