Skip to content

Commit 91beb4a

Browse files
committed
Palindrome partitioning II top down solution implemented
1 parent 9740419 commit 91beb4a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Diff for: source-code/Palindrome_Partitioning_II.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
class Solution {
2+
int minCut(int i, int j, string const& s, vector<vector<int>>& dp) {
3+
if(i >= j) {
4+
return 0;
5+
}
6+
if(dp[i][j] != INT_MAX) {
7+
return dp[i][j];
8+
}
9+
if(s[i] == s[j] and minCut(i + 1, j - 1, s, dp) == 0) {
10+
return dp[i][j] = 0;
11+
}
12+
int Min = j - i;
13+
for(int k = i; k < j; k++) {
14+
Min = min(Min, 1 + minCut(i, k, s, dp) + minCut(k + 1, j, s, dp));
15+
}
16+
return dp[i][j] = Min;
17+
}
218
public:
319
int minCut(string s) {
20+
// vector<vector<int>> dp(s.length(), vector<int>(s.length(), INT_MAX));
21+
// return minCut(0, s.length() - 1, s, dp);
22+
423
const int SIZE = s.length();
524
vector<int> minCuts(SIZE + 1, 0);
625
for (int i = 0; i <= SIZE; i++) {

0 commit comments

Comments
 (0)