Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Palindromic Partitioning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// User function Template for C++

class Solution{
public:
bool isPalindrome(int i, int j, string& str) {
while(i<j) {
if(str[i]!=str[j]) return false;
i++,j--;
}
return true;
}
int solve(int i,int n,string &str,vector<int>&dp) {
if(i==n) {
return 0;
}
int mini = INT_MAX;
if(dp[i]!=-1) return dp[i];

for(int j = i; j<n; j++) {
if(isPalindrome(i,j,str)) {
int cost = 1 + solve(j+1, n, str,dp);
mini = min(mini,cost);
}
}
return dp[i]=mini;
}
int palindromicPartition(string str)
{
int n = str.size();
vector<int>dp(n,-1);
return solve(0,n,str,dp)-1;
}
};