-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathsolve.cpp
More file actions
30 lines (30 loc) · 687 Bytes
/
Copy pathsolve.cpp
File metadata and controls
30 lines (30 loc) · 687 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
#include <vector>
#include <string>
#include <unordered_set>
#include <iostream>
using namespace std;
class Solution {
public:
bool wordBreak(string s, const unordered_set<string> &dict) {
int n = s.size();
vector<bool> canBreak(n + 1, false);
canBreak[0] = true;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
if (canBreak[j] && dict.find(s.substr(j, i - j)) != dict.end()) {
canBreak[i] = true;
break;
}
}
}
return canBreak[n];
}
};
int main(int argc, char **argv)
{
Solution solution;
string s = "leetcode";
unordered_set<string> dict = {"leet", "code"};
cout << solution.wordBreak(s, dict) << endl;
return 0;
}