-
Notifications
You must be signed in to change notification settings - Fork 0
/
63) Word break Problem.cpp
61 lines (51 loc) · 1.14 KB
/
63) Word break Problem.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
53
54
55
56
57
58
59
60
61
//{ Driver Code Starts
//Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
// A : given string to search
// B : vector of available strings
class Solution
{
public:
//static method
bool static check(string a, string b){
return a.size() > b.size();
}
int wordBreak(string A, vector<string> &B) {
//sort the string
sort(B.begin(), B.end(), check);
int i=0;
int idx;
while(i<B.size() && A.size() > 0){
while((idx = A.find(B[i])) != string::npos){
A.replace(idx, B[i].size(),"");
}
i++;
}
if(A.size() == 0){
return 1;
}
}
};
//{ Driver Code Starts.
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<string> dict;
for(int i=0;i<n;i++){
string S;
cin>>S;
dict.push_back(S);
}
string line;
cin>>line;
Solution ob;
cout<<ob.wordBreak(line, dict)<<"\n";
}
}
// } Driver Code Ends