-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordbreak.cpp
More file actions
34 lines (30 loc) · 855 Bytes
/
wordbreak.cpp
File metadata and controls
34 lines (30 loc) · 855 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
bool inList(vector<string> &list, string word){
for(int i=0 ; i<list.size(); i++){
if(list[i].compare(word) == 0){
return true;
}
}
return false;
}
int wordBreak(string A, vector<string> &B) {
bool dp[A.size() + 1] = {false};
for(int i=1; i<=A.size(); i++){
if(dp[i] == false and inList(B, A.substr(0, i))){
dp[i] = true;
}
if(dp[i] == true){
if(i == A.size()){
return true;
}
for(int j=i+1; j<=A.size(); j++){
if(dp[j] == false and inList(B, A.substr(i, j-i))){
dp[j] = true;
}
if(j == A.size() and dp[j] == true){
return true;
}
}
}
}
return false;
}