-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordBreak.cpp
52 lines (50 loc) · 1.59 KB
/
wordBreak.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
42
43
44
45
46
47
48
49
50
51
52
bool wordBreak(string s, unordered_set<string> &dict) {
vector<vector<int>> table(s.size(), vector<int>(s.size(), INT_MAX));
for(int i=0; i<s.size(); i++) {
string word = s.substr(i, 1);
if(dict.find(word)!=dict.end()) {
table[i][i] = 1;
}else{
table[i][i] = 0;
}
}
// using the length as the progression method
// traverse the triangle of the status matrix
for(int len=2; len<=s.size(); len++) {
for(int i=0; i<=(s.size()-len); i++) {
int j = i+len-1;
// see if s<i,j> is a word
string word = s.substr(i, len);
if(dict.find(word)!=dict.end()) {
table[i][j]=1; continue;
}
// see if s<i,k> and s<k+1,j> have combinations
for(int k=i; k<j; k++) {
if(table[i][k]&&table[k+1][j]) {
table[i][j]=1;
break;
}
}
// if it wasn't updated it means it doesn't have a combination
if(table[i][j]==INT_MAX) {
table[i][j]=0;
}
}
}
// return the result from the right corner of the status matrix
return table[0][s.size()-1];
}
/*
status update loop invariant:
( v[i,k] && v[k+1,j] )|| substr[i,j]
j
_______________
i |_|_ |
| |_|_ |
| |_|_ |
| |_|_ |
| |_|_ |
| |_|_ |
| |_|_|
|_____________|_|
*/