forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path132. Palindrome Partitioning II.cpp
184 lines (108 loc) · 3.43 KB
/
132. Palindrome Partitioning II.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
Example 1:
Input: s = "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
Example 2:
Input: s = "a"
Output: 0
Example 3:
Input: s = "ab"
Output: 1
Constraints:
1 <= s.length <= 2000
s consists of lower-case English letters only.
// Recusive Memoized + Optimized (Accepted)
class Solution {
public:
vector<vector<int>> dp;
bool isPalindrome(int start, int end, string &s) {
while (start < end) {
if (s[start++] != s[end--]) return false;
}
return true;
}
int solve (int i, int j, string &s){
if(i>=j || isPalindrome(i, j, s)) return 0;
if(dp[i][j]!=-1) return dp[i][j];
int res = INT_MAX;
for (int k = i; k < j; k++){
if (isPalindrome(i, k, s)){
int tmp = solve (k + 1, j, s) + 1;
res = min (res, tmp);
}
}
return dp[i][j] = res;
}
int minCut(string s) {
int n = s.length();
dp.resize(n, vector<int> (n, -1));
return solve (0, n - 1, s);
}
};
// REcursive Memoized (TLE)
class Solution {
public:
vector<vector<int>> dp;
bool isPalindrome(int start, int end, string s) {
while (start < end) {
if (s[start++] != s[end--]) return false;
}
return true;
}
int solve(int i, int j, string s) {
if (i >= j) return 0;
if(isPalindrome(i, j, s)) return 0;
if (dp[i][j] != -1) return dp[i][j];
int res = INT_MAX;
int left, right;
for (int k = i; k <= j - 1; k++) {
if (dp[i][k] != -1) left = dp[i][k];
else dp[i][k] = left = solve(i, k, s);
if (dp[k + 1][j] != -1) right = dp[k + 1][j];
else dp[k + 1][j] = right = solve(k + 1, j, s);
int tmp = left + right + 1;
res = min(res, tmp);
}
return dp[i][j] = res;
}
int minCut(string s) {
int n = s.length();
dp.resize(n, vector<int> (n, -1));
return solve(0, n - 1, s);
}
};
// All palindrome finding - backtracking(TLE)
class Solution {
public:
bool isPalindrome(int start, int end, string s) {
while (start < end) {
if (s[start++] != s[end--]) return false;
}
return true;
}
void dfs(int start, string s, vector<string> &cur, vector<vector<string>> &all) {
if (start == s.length()) {
all.push_back(cur);
return;
}
for (int i = start; i < s.length(); i++) {
if (isPalindrome(start, i, s)) {
cur.push_back(s.substr(start, i - start + 1));
dfs(i + 1, s, cur, all);
cur.pop_back(); // backtrack
}
}
}
int minCut(string s) {
vector<vector<string>> all;
vector<string> cur;
dfs(0, s, cur, all);
int res = INT_MAX;
for (auto &x: all) {
res = min(res, (int)x.size());
}
return res - 1;
}
};