forked from nishitpanchal395/projecthactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordBreak.cpp
More file actions
53 lines (44 loc) · 1.26 KB
/
Copy pathWordBreak.cpp
File metadata and controls
53 lines (44 loc) · 1.26 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
// Word Break Medium
// Given a string A and a dictionary of n words B, find out if A can be segmented into a space-separated sequence of dictionary words.
// Note: From the dictionary B each word can be taken any number of times and in any order.
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int wordBreak(string A, vector<string> &B) {
unordered_map<string, int> dypr;
int len = A.length();
if(len == 0) return 1;
if(dypr[A] !=0) return dypr[A];
for(int i=1; i<=len;i++){
int flag =0;
string str = A.substr(0,i);
for(int j=0; j<B.size();j++){
if(str.compare(B[j]) == 0){
flag =1; break;
}
}
if(flag == 1 and wordBreak(A.substr(i, len-i), B) == 1) return dypr[A]=1;
}
return dypr[A]= 0;
}
};
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";
}
}