forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
length-of-the-longest-valid-substring.cpp
64 lines (57 loc) · 1.69 KB
/
length-of-the-longest-valid-substring.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
// Time: O((m + n) * l), n = len(word), m = len(forbidden), l = max(len(w) for w in forbidden)
// Space: O(t), t is the size of trie
// two pointers, sliding window, trie
class Solution {
public:
int longestValidSubstring(string word, vector<string>& forbidden) {
Trie trie;
for (const auto& w : forbidden) {
trie.insert(w);
}
int result = 0;
for (int left = size(word) - 1, right = size(word) - 1; left >= 0; --left) {
for (int i = left, curr = 0; i <= right; ++i) {
curr = trie.child(curr, word[i]);
if (!curr) { // O(l) times
break;
}
if (trie.is_string(curr)) {
right = i - 1;
break;
}
}
result = max(result, right - left + 1);
}
return result;
}
private:
class Trie {
public:
Trie() {
create_node();
}
void insert(const string& s) {
int curr = 0;
for (const auto& c : s) {
if (!nodes_[curr][c - 'a']) {
nodes_[curr][c - 'a'] = create_node();
}
curr = nodes_[curr][c - 'a'];
}
++nodes_[curr].back();
}
int child(int curr, char c) const {
return nodes_[curr][c - 'a'];
}
bool is_string(int curr) const {
return nodes_[curr].back();
}
private:
int create_node() {
nodes_.emplace_back(26 + 1);
return size(nodes_) - 1;
}
using TrieNode = vector<int>;
vector<TrieNode> nodes_;
};
};