forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
37 lines (29 loc) · 806 Bytes
/
main2.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
/// Source : https://leetcode.com/problems/word-break/
/// Author : liuyubobobo
/// Time : 2021-02-25
#include <iostream>
#include <vector>
using namespace std;
/// DP
/// Time Complexity: O(|s| * |wordDict|)
/// Space Complexity: O(|s|)
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
vector<bool> dp(s.size() + 1, false);
dp[s.size()] = true;
for(int i = dp.size() - 1; i >= 0; i --){
int left = s.size() - i;
for(const string& word: wordDict)
if(left >= word.size() && s.substr(i, word.size()) == word &&
dp[i + word.size()]){
dp[i] = true;
break;
}
}
return dp[0];
}
};
int main() {
return 0;
}