-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordBreak.h
59 lines (56 loc) · 1.25 KB
/
WordBreak.h
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
/**
*Solution 1
*dp
*time:O(n^2) space:O(n)
*/
class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
vector<bool> dp(s.size()+1,false);
dp[0]=true;
for(int i=1;i<=s.size();++i)
{
for(int j=i-1;j>=0;--j)
{
if(dp[j]&&dict.find(s.substr(j,i-j))!=dict.end())
{
dp[i]=true;
break;
}
}
}
return dp[s.size()];
}
};
/**
*Solution 2
*dfs,穷举dict,超时
*time:O(2^n) space:O(n)
*/
class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
auto endOfDIct=end(dict);
for(auto it=dict.begin();it!=endOfDIct;++it)
{
int len=it->size();
if(s.substr(0,len)==*it&&wordBreak(s.substr(len),dict))return true;
}
return false;
}
};
/**
*Solution 3
*dfs,穷举substr,超时
*time:O(2^n) space:O(n)
*/
class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
for(int i=1;i<=s.size();++i)
{
if(dict.find(s.substr(0,i))!=dict.end()&&wordBreak(s.substr(i),dict))return true;
}
return false;
}
};