forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string-matching-in-an-array.cpp
201 lines (185 loc) · 5.82 KB
/
string-matching-in-an-array.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Time: O(n + m + z) = O(n), n is the total size of patterns
// , m is the total size of query string
// , z is the number of all matched strings
// , O(n) = O(m) = O(z) in this problem
// Space: O(t), t is the total size of ac automata trie
struct AhoNode {
vector<AhoNode *> children;
vector<int> indices;
AhoNode *suffix;
AhoNode *output;
AhoNode() :
children(26, nullptr),
suffix(nullptr),
output(nullptr) {}
};
class AhoTrie {
public:
AhoTrie(const vector<string>& patterns) : root_(createACTrie(patterns)) {
node_ = createACSuffixAndOutputLinks(root_);
}
vector<int> step(char letter) {
while (node_ && !node_->children[letter - 'a']) {
node_ = node_->suffix;
}
node_ = node_ ? node_->children[letter - 'a'] : root_;
return getACNodeOutputs(node_);
}
void reset() {
node_ = root_;
}
private:
AhoNode *createACTrie(const vector<string>& patterns) { // Time: O(n), Space: O(t)
auto root = new AhoNode();
for (int i = 0; i < patterns.size(); ++i) {
auto node = root;
for (const auto& c : patterns[i]) {
if (!node->children[c - 'a']) {
node->children[c - 'a'] = new AhoNode();
}
node = node->children[c - 'a'];
}
node->indices.emplace_back(i);
}
return root;
}
AhoNode *createACSuffixAndOutputLinks(AhoNode *root) { // Time: O(n), Space: O(t)
queue<AhoNode *> q;
for (auto node : root->children) {
if (!node) {
continue;
}
q.emplace(node);
node->suffix = root;
}
while (!q.empty()) {
auto node = q.front(); q.pop();
for (int c = 0; c < node->children.size(); ++c) {
if (!node->children[c]) {
continue;
}
auto child = node->children[c];
q.emplace(child);
auto suffix = node->suffix;
while (suffix && !suffix->children[c]) {
suffix = suffix->suffix;
}
child->suffix = suffix ? suffix->children[c] : root;
child->output = !child->suffix->indices.empty() ?
child->suffix : child->suffix->output;
}
}
return root;
}
vector<int> getACNodeOutputs(AhoNode *node) { // Time: O(z)
vector<int> result;
for (const auto& i : node_->indices) {
result.emplace_back(i);
// return result;
}
auto output = node_->output;
while (output) {
for (const auto& i : output->indices) {
result.emplace_back(i);
// return result;
}
output = output->output;
}
return result;
}
AhoNode * const root_;
AhoNode *node_;
};
class Solution {
public:
vector<string> stringMatching(vector<string>& words) {
AhoTrie trie(words);
unordered_set<int> lookup;
for (int i = 0; i < words.size(); ++i) {
trie.reset();
for (const auto& c : words[i]) {
for (const auto& j : trie.step(c)) {
if (j != i) {
lookup.emplace(j);
}
}
}
}
vector<string> result;
for (const auto& i : lookup) {
result.emplace_back(words[i]);
}
return result;
}
};
// Time: O(n^2 * l), n is the number of strings
// Space: O(l) , l is the max length of strings
class Solution2 {
public:
vector<string> stringMatching(vector<string>& words) {
vector<string> result;
for (int i = 0; i < words.size(); ++i) {
const auto& prefix = getPrefix(words[i]);
for (int j = 0; j < words.size(); ++j) {
if (i != j && kmp(words[j], words[i], prefix) != -1) {
result.emplace_back(words[i]);
break;
}
}
}
return result;
}
private:
int kmp(const string& text, const string& pattern, const vector<int>& prefix) {
if (pattern.empty()) {
return 0;
}
if (text.length() < pattern.length()) {
return -1;
}
int j = -1;
for (int i = 0; i < text.length(); ++i) {
while (j != -1 && pattern[j + 1] != text[i]) {
j = prefix[j];
}
if (pattern[j + 1] == text[i]) {
++j;
}
if (j + 1 == pattern.length()) {
return i - j;
}
}
return -1;
}
vector<int> getPrefix(const string& pattern) {
vector<int> prefix(pattern.length(), -1);
int j = -1;
for (int i = 1; i < pattern.length(); ++i) {
while (j != -1 && pattern[j + 1] != pattern[i]) {
j = prefix[j];
}
if (pattern[j + 1] == pattern[i]) {
++j;
}
prefix[i] = j;
}
return prefix;
}
};
// Time: O(n^2 * l^2), n is the number of strings
// Space: O(1) , l is the max length of strings
class Solution3 {
public:
vector<string> stringMatching(vector<string>& words) {
vector<string> result;
for (int i = 0; i < words.size(); ++i) {
for (int j = 0; j < words.size(); ++j) {
if (i != j && words[j].find(words[i]) != string::npos) {
result.emplace_back(words[i]);
break;
}
}
}
return result;
}
};