-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path139.WordBreak.h
More file actions
74 lines (57 loc) · 1.76 KB
/
139.WordBreak.h
File metadata and controls
74 lines (57 loc) · 1.76 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
/*
bluepp
2014-07-02
2014-07-30
2014-08-16
2014-09-20
2014-11-23
May the force be with me!
Problem: Word Break
Source: http://oj.leetcode.com/problems/word-break/
Notes:
Given a string s and a dictionary of words dict, determine if s can be segmented into
a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
Solution: dp.
*/
/* memo */
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
vector<int> memo(s.size(), -1);
return _check(s, wordSet, 0, memo);
}
bool _check(string s, unordered_set<string> &wordSet, int start, vector<int>& memo) {
if (start >= s.size()) {
return true;
}
if (memo[start] != -1) {
return memo[start];
}
for (int i = start+1; i <= s.length(); i++) {
if (wordSet.count(s.substr(start, i-start)) && _check(s, wordSet, i, memo)) {
return memo[start] = 1;
}
}
return memo[start] = 0;
}
bool wordBreak(string s, unordered_set<string> &dict) {
int n = s.size();
bool dp[n+1];
memset(dp, false, sizeof(dp));
dp[n] = true;
for (int i = n; i >= 0; i--)
{
for (int j = i+1; j <= n ; j ++)
{
if (dp[j] && dict.find(s.substr(i, j-i)) != dict.end())
{
dp[i] = true;
break;
}
}
}
return dp[0];
}