-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword_break_2.cpp
More file actions
59 lines (51 loc) · 1.43 KB
/
word_break_2.cpp
File metadata and controls
59 lines (51 loc) · 1.43 KB
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
50
51
52
53
54
55
56
57
58
59
class Solution {
public:
vector<vector<int> > genTable(string &s, unordered_set<string> &dict)
{
int n = s.length();
vector<vector<int> > tbl(n);
for (int i = n - 1; i >= 0; i--)
{
if(dict.count(s.substr(i))) tbl[i].push_back(n-1);
}
for (int i = n - 2; i >= 0; i--)
{
// if we can break into i+1
// continue with i
if (!tbl[i+1].empty())//if we can break i->n
{
for (int j = i, d = 1; j >= 0 ; j--, d++)
{
if (dict.count(s.substr(j, d))) tbl[j].push_back(i); // see if we can break j->i
}
}
}
return tbl;
}
void Helper(vector<string> &result, string &s, string &tmp, int start, vector<vector<int>> &t2, unordered_set<string> &dict)
{
if(start==s.size() ) {
result.push_back(tmp);
return;
}
for(int i=0; i< t2[start].size(); i++) {
string sub = s.substr(start, t2[start][i]-start+1);
string newtmp = tmp;
if(newtmp!=""){
newtmp =newtmp +" "+ sub;
}else {
newtmp = sub;
}
int nextstart = t2[start][i]+1;
Helper(result, s, newtmp, nextstart, t2, dict);
}
return;
}
vector<string> wordBreak(string s, unordered_set<string> &dict) {
vector<vector<int>> t2 = genTable(s, dict);
string tmp="";
vector<string> result;
Helper(result, s, tmp, 0, t2, dict);
return result;
}
};