forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefix-and-suffix-search.cpp
81 lines (71 loc) · 2.17 KB
/
prefix-and-suffix-search.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
80
81
// Time: ctor: O(w * l), w is the number of words, l is the word length on average
// search: O(l + m + n), m is the number of prefix match, n is the number of suffix match
// Space: O(w * l)
struct TrieNode {
vector<int> words; // index of words
vector<TrieNode *> leaves;
TrieNode() : leaves(26) {}
void insert(const string& s, const int i) {
auto* p = this;
p->words.emplace_back(i);
for (const auto& c : s) {
if (!p->leaves[c - 'a']) {
p->leaves[c - 'a'] = new TrieNode;
}
p = p->leaves[c - 'a'];
p->words.emplace_back(i);
}
}
const vector<int>& find(const string& s) const {
auto* p = this;
for (const auto& c : s) {
if (!p->leaves[c - 'a']) {
static const vector<int> empty;
return empty;
}
p = p->leaves[c - 'a'];
}
return p->words;
}
~TrieNode() {
for (auto& node : leaves) {
if (node) {
delete node;
}
}
}
};
class WordFilter {
public:
WordFilter(vector<string> words) {
for (int i = words.size() - 1; i >= 0; --i) {
auto word = words[i];
prefix_trie_.insert(word, i);
reverse(word.begin(), word.end());
suffix_trie_.insert(word, i);
}
}
int f(string prefix, string suffix) {
const auto& prefix_match = prefix_trie_.find(prefix);
reverse(suffix.begin(), suffix.end());
const auto& suffix_match = suffix_trie_.find(suffix);
int i = 0, j = 0;
while (i != prefix_match.size() && j != suffix_match.size()) {
if (prefix_match[i] == suffix_match[j]) {
return prefix_match[i];
} else if (prefix_match[i] > suffix_match[j]) {
++i;
} else {
++j;
}
}
return -1;
}
private:
TrieNode prefix_trie_, suffix_trie_;
};
/**
* Your WordFilter object will be instantiated and called as such:
* WordFilter obj = new WordFilter(words);
* int param_1 = obj.f(prefix,suffix);
*/