-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0139.cpp
70 lines (62 loc) · 1.67 KB
/
0139.cpp
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
class Solution {
public:
bool wordBreak(string s, vector<string> &wordDict) {
// return func1(s, wordDict);
return func2(s, wordDict);
}
// ** 2D array dp
// ** dp[i][j] stores s[i, j] is in-table or not
// ** time O(N^3), AC but not fast enough
bool func1(string s, vector<string> &wordDict) {
int N = s.length();
if (N <= 0) {
return true;
}
vector<vector<bool>> dp(N, vector<bool>(N, false));
unordered_set<string> dict(wordDict.begin(), wordDict.end());
for (int i = 0; i < N; i++) {
if (dict.count(string(1, s[i])) > 0) {
dp[i][i] = true;
}
}
for (int len = 2; len <= N; len++) {
for (int lo = 0, hi = len - 1; hi < N; lo++, hi++) {
if (dict.count(s.substr(lo, len)) > 0) {
dp[lo][hi] = true;
continue;
}
for (int mid = lo; mid < hi; mid++) {
if (dp[lo][mid] && dp[mid + 1][hi]) {
dp[lo][hi] = true;
break;
}
}
}
}
return dp[0][N - 1];
}
// ** 1D array dp
// ** dp[i] stores s[0, i] is in-table or not
// ** time O(N^2) if ignore substr(), AC really fast
bool func2(string s, vector<string> &wordDict) {
int N = s.length();
if (N <= 0) {
return true;
}
vector<bool> dp(N, false);
unordered_set<string> dict(wordDict.begin(), wordDict.end());
for (int i = 0; i < N; i++) {
if (dict.count(s.substr(0, i + 1)) > 0) {
dp[i] = true;
continue;
}
for (int j = i; j > 0; j--) {
if (dp[j - 1] && dict.count(s.substr(j, i - j + 1)) > 0) {
dp[i] = true;
break;
}
}
}
return dp[N - 1];
}
};