-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path139.cpp
More file actions
89 lines (83 loc) · 2.46 KB
/
139.cpp
File metadata and controls
89 lines (83 loc) · 2.46 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class Solution {
public:
vector<int> dp;
bool recursive(string s, unordered_set<string>& ust, int idx, int n){
if(idx>=n)
return true;
if(dp[idx]!=-1)
return dp[idx];
for(int i = idx; i<n; i++){
string tmp = s.substr(idx,i-idx+1);
if(ust.count(tmp) && recursive(s,ust,i+1,n)){
dp[idx] = 1;
return true;
}
}
dp[idx] = 0;
return false;
}
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> ust(wordDict.begin(), wordDict.end());
int n = s.size();
dp.resize(n+1,-1);
return recursive(s,ust,0,n);
}
};
class Solution {// S = s1 + s2 if(s1 and s2 int wordDict then S is also in wordDict)
public:
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> ust(wordDict.begin(),wordDict.end());
int n = s.size();
vector<bool> dp(n+1, false);
dp[0] = true;
for(int length = 1; length<=n; length++){
for(int i=0; i<length;i++){
if(dp[i] && ust.count(s.substr(i,length-i))){
dp[length] = true;
break;
}
}
}
return dp[n];
}
};
class Solution {// O(nk)
public:
bool wordBreak(string s, vector<string>& wordDict) {
vector<bool> dp(s.size()+1, false);
dp[0] = true;
for(int i = 0;i<s.size();i++){
if(dp[i]){
for(auto& j: wordDict){
if(!dp[i+j.size()] && s.substr(i, j.size())==j){
dp[i+j.size()] = true;
}
}
}
}
return dp.back();
}
};
class Solution {//BFS O(n2) o(n);
public:
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> ust(wordDict.begin(),wordDict.end());
vector<bool> visited(s.size(),0);
queue<int> q;
q.push(0);
while(!q.empty()){
int start = q.front();q.pop();
if(!visited[start]){
for(int en = start +1; en<=s.size(); en++){
if(ust.count(s.substr(start,en-start))){
q.push(en);
if(en == s.size())
return true;
}
}
visited[start] = 1;
}
}
return false;
}
};