-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path139.cpp
More file actions
23 lines (23 loc) · 725 Bytes
/
Copy path139.cpp
File metadata and controls
23 lines (23 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
bool wordBreak(string s, vector<string> &wordDict) {
int n = s.size();
if (n == 0)
return true;
map<string, bool> dict;
for (int i = 0; i < wordDict.size(); ++i)
dict.insert(map<string, bool>::value_type(wordDict[i], true));
vector<bool> dp(n + 1, false);
dp[0] = true;
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= i; ++j) {
string temp = s.substr(j, i - j + 1);
if (dict.find(temp) != dict.end() && (dp[j] == true || j == 0)) {
dp[i + 1] = true;
break;
}
}
}
return dp[n];
}
};