forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
139.cpp
41 lines (36 loc) · 1.05 KB
/
139.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
38
39
40
41
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
int len = s.size();
vector<bool> possible(len, 0);
for (int i = 0; i < len; i++) {
if (dict.find(s.substr(0, i + 1)) != dict.end())
possible[i] = true;
for (int j = 0; j < i; j++) {
if (possible[j] &&
dict.find(s.substr(j + 1, i - j)) != dict.end()) {
possible[i] = true;
break;
}
}
}
/*
for (vector<bool>::iterator it = possible.begin();
it != possible.end(); it++)
cout << *it << endl;
*/
return possible[len - 1];
}
};
int main() {
Solution solution;
string s = "leetcode";
unordered_set<string> dict = { "leet", "code" };
cout << solution.wordBreak(s, dict) << endl;
return 0;
}