-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord_Break.cpp
More file actions
49 lines (48 loc) · 1.34 KB
/
Word_Break.cpp
File metadata and controls
49 lines (48 loc) · 1.34 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
class Solution {
public:
bool wordBreak(string A, vector<string>& B) {
unordered_set<string> s(B.begin(), B.end());
int n = A.size();
vector<int> tmp(n+1,0);
tmp[n] = 1;
for(int i = n-1; i >= 0; i--){
for(int j = i; j < n; j++){
string t = A.substr(i, j-i+1);
if(s.find(t)!=s.end() && tmp[j+1] == 1){
tmp[i] = 1;
break;
}
}
}
return tmp[0];
}
};
class Solution2 {
public:
bool recursive(string s, unordered_set<string>& ust){
int size = s.size();
if(size==0)
return true;
bool dp[size+1];
memset(dp,0,sizeof(dp));
for(int i=1; i<=size; i++){
if(!dp[i] && (ust.find(s.substr(0,i))!=ust.end()))
dp[i] = true;
if(dp[i]){
if(i==size)
return true;
for(int j=i+1; j<=size; j++){
if(!dp[j] && (ust.find(s.substr(i,j-i))!=ust.end()))
dp[j] = true;
if(j==size && dp[j])
return true;
}
}
}
return false;
}
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> ust(wordDict.begin(), wordDict.end());
return recursive(s,ust);
}
};