-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
38 lines (35 loc) · 1015 Bytes
/
Solution.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
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <functional>
#include <iostream>
#include <limits>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
class Solution {
public:
int minExtraChar(std::string s, std::vector<std::string>& dictionary) {
const size_t n{s.length()};
// Naively, let dp[i] represent the minimum extra characters left over by
// breaking up s[:i].
// Therefore, the minimum extra chars of s[:i] will be the minimum of
// breaking up (s[:i-1] + 1), and dp[j] if s[j:i] is in the dictionary
std::vector<int> dp(n + 1, n);
dp[0] = 0;
for (int i = 1; i <= n; ++i) {
dp[i] = dp[i - 1] + 1;
for (const std::string& word : dictionary) {
if (i >= word.length() &&
s.substr(i - word.length(), word.length()) == word) {
dp[i] = std::min(dp[i], dp[i - word.length()]);
}
}
}
return dp[n];
}
};