-
Notifications
You must be signed in to change notification settings - Fork 3
/
745.cpp
79 lines (75 loc) · 1.89 KB
/
745.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
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
75
76
77
78
79
class TrieNode {
public:
vector<TrieNode*> children;
int isEnd;
TrieNode() {
children.resize(27);
isEnd = -1;
}
};
class Trie {
private:
TrieNode* root;
public:
Trie() {
root = new TrieNode();
}
int char2idx(char c) {
if (c == '#') return 26;
return c - 'a';
}
void insert(string& s, int index) {
TrieNode* curr = root;
for (auto& c : s) {
int idx = char2idx(c);
if (curr->children[idx] == nullptr) {
curr->children[idx] = new TrieNode();
}
curr = curr->children[idx];
}
curr->isEnd = index;
}
void dfs(TrieNode* node, int& res) {
if (node->isEnd >= 0) res = max(res, node->isEnd);
for (auto& child : node->children) {
if (child != nullptr) dfs(child, res);
}
}
void _search(string& s, int& res) {
TrieNode* curr = root;
for (auto& c : s) {
int idx = char2idx(c);
if (curr->children[idx] == nullptr) return;
curr = curr->children[idx];
}
dfs(curr, res);
}
int search(string& s) {
int res = -1;
_search(s, res);
return res;
}
};
class WordFilter {
public:
Trie* trie;
WordFilter(vector<string>& words) {
trie = new Trie();
for (int i = 0; i < words.size(); ++i) {
string& word = words[i];
for (int j = 0; j < word.size(); ++j) {
string extend = word.substr(j) + "#" + word;
trie->insert(extend, i);
}
}
}
int f(string prefix, string suffix) {
string query = suffix + "#" + prefix;
return trie->search(query);
}
};
/**
* Your WordFilter object will be instantiated and called as such:
* WordFilter* obj = new WordFilter(words);
* int param_1 = obj->f(prefix,suffix);
*/