forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extra-characters-in-a-string.cpp
64 lines (57 loc) · 1.6 KB
/
extra-characters-in-a-string.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((n + m) * l), l is max(len(w) for w in dictionary)
// Space: O(n + t)
// trie, dp
class Solution {
public:
int minExtraChar(string s, vector<string>& dictionary) {
Trie trie;
for (const auto& w : dictionary) {
trie.insert(w);
}
vector<int> dp(size(s) + 1, numeric_limits<int>::max());
dp[0] = 0;
for (int i = 0; i < size(s); ++i) {
dp[i + 1] = min(dp[i + 1], dp[i] + 1);
for (int j = i, curr = 0; j < size(s); ++j) {
curr = trie.child(curr, s[j]);
if (!curr) {
break;
}
if (trie.is_string(curr)) {
dp[j + 1] = min(dp[j + 1], dp[i]);
}
}
}
return dp.back();
}
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_;
};
};