forked from royalpranjal/Interview-Bit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordBreak.cpp
More file actions
34 lines (27 loc) · 841 Bytes
/
Copy pathWordBreak.cpp
File metadata and controls
34 lines (27 loc) · 841 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 search(string A, vector<string> B){
for(int i = 0; i < B.size(); i++){
if(B[i] == A){
return true;
}
}
return false;
}
int Solution::wordBreak(string A, vector<string> &B) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
int n = A.size();
vector<int> temp(n+1, 0);
temp[n] = 1;
for(int i = n-1; i >= 0; i--){
for(int j = i; j < n; j++){
string s = A.substr(i, j-i+1);
if(search(s, B) && temp[j+1] == 1){
temp[i] = 1;
break;
}
}
}
return temp[0];
}