-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromic Partitioning.cpp
76 lines (65 loc) · 1.8 KB
/
Palindromic Partitioning.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
/*
Problem Link: https://practice.geeksforgeeks.org/problems/palindromic-patitioning4845/1
*/
// -----Approach 1: Memoization ------------------------------------------------------------
class Solution{
private:
bool validPalindrome(int i, int j, string s){
while(i < j){
if(s[i] != s[j]) return false;
i++;
j--;
}
return true;
}
int sol(int i, string s, vector<int>& dp){
int n= s.size();
if(i == n) return 0;
if(dp[i] != -1) return dp[i];
int mi= 1e9;
for(int j=i; j<n; j++){
if(validPalindrome(i, j, s)){
int cost= 1 + sol(j+1, s, dp);
mi= min(mi, cost);
}
}
return dp[i]= mi;
}
public:
int palindromicPartition(string s)
{
int n= s.size();
vector<int> dp(n, -1);
return sol(0, s, dp) - 1; // we are also partitioning at the very end, so subtract 1
}
};
// -----Approach 2: Tabulation ------------------------------------------------------------
// User function Template for C++
class Solution{
private:
bool validPalindrome(int i, int j, string s){
while(i < j){
if(s[i] != s[j]) return false;
i++;
j--;
}
return true;
}
public:
int palindromicPartition(string s)
{
int n= s.size();
vector<int> dp(n+1, 0);
for(int i=n-1; i>=0; i--){
int mi= 1e9;
for(int j=i; j<n; j++){
if(validPalindrome(i, j, s)){
int cost= 1 + dp[j+1];
mi= min(mi, cost);
}
}
dp[i]= mi;
}
return dp[0] - 1; // we are also partitioning at the very end, so subtract 1
}
};