leetcode/lcci/17.13.Re-Space/Solution.cpp

17 lines
516 B
C++

class Solution {
public:
int respace(vector<string>& dictionary, string sentence) {
unordered_set<string> s(dictionary.begin(), dictionary.end());
int n = sentence.size();
vector<int> dp(n + 1);
for (int i = 1; i <= n; ++i) {
dp[i] = dp[i - 1] + 1;
for (int j = 0; j < i; ++j) {
if (s.count(sentence.substr(j, i - j))) {
dp[i] = min(dp[i], dp[j]);
}
}
}
return dp[n];
}
};