-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord_Break.cc
More file actions
49 lines (42 loc) · 1008 Bytes
/
Copy pathWord_Break.cc
File metadata and controls
49 lines (42 loc) · 1008 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <vector>
#include <iostream>
#include <unordered_set>
#include <string>
using namespace std;
class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict);
};
bool Solution::wordBreak(string s, unordered_set<string> &dict) {
int len = s.length();
if (len == 0 || dict.size() == 0) return false;
bool *d = new bool[len+1];
for (int i=0; i <= len; ++i)
d[i] = false;
d[0] = true;
for (int i=0; i < len; ++i) {
if (d[i]) {
int k = 1;
for (int j=i; j < len; ++j) {
if (dict.find(string(s, i, k)) != dict.end()) {
d[j+1] = true;
}
++k;
}
}
}
bool ret = d[len];
delete[] d;
return ret;
}
int main(int argc, char **argv) {
Solution s;
string str("Ilikeamotorbike");
unordered_set<string> dict;
dict.insert(string("I"));
dict.insert(string("like"));
dict.insert(string("a"));
dict.insert(string("moto"));
dict.insert(string("bike"));
cout << s.wordBreak(str, dict) << endl;
}