mirror of https://github.com/doocs/leetcode.git
17 lines
516 B
C++
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];
|
|
}
|
|
}; |