-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path139.cpp
More file actions
30 lines (27 loc) · 797 Bytes
/
139.cpp
File metadata and controls
30 lines (27 loc) · 797 Bytes
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
// brute force
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
vector<bool> dp(s.size(), false);
// [j, i)
// i -> end index
for (int i = 1; i <= s.size(); ++i) {
// j -> start index
for (int j = 0; j < i; ++j) {
string substring = s.substr(j, i-j);
if (contain(wordDict, substring) && (j == 0 || dp[j-1])) {
dp[i-1] = true;
}
}
}
return dp[s.size()-1];
}
bool contain(vector<string>& wordDict, string substring) {
for (int i = 0; i < wordDict.size(); ++i) {
if (wordDict[i] == substring) {
return true;
}
}
return false;
}
};