From 24bd29addc369dd81074fde5a817a5a3de5c5516 Mon Sep 17 00:00:00 2001 From: rakshil14-2 <56144321+rakshil14-2@users.noreply.github.com> Date: Sat, 1 Oct 2022 22:03:10 +0530 Subject: [PATCH] Create palindromicpartion.cpp --- C++/palindromicpartion.cpp | 97 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 C++/palindromicpartion.cpp diff --git a/C++/palindromicpartion.cpp b/C++/palindromicpartion.cpp new file mode 100644 index 00000000..0406f70b --- /dev/null +++ b/C++/palindromicpartion.cpp @@ -0,0 +1,97 @@ +//{ Driver Code Starts +// Initial Template for c++ + +#include +using namespace std; + +// } Driver Code Ends +// User function Template for C++ + +class Solution{ +public: +int t[501][501]; + bool palindrome(string s1,int i ,int j) + { + + if(i==j) + return true; + if(i>j) + return true; + + while(i=j) + return 0; + + + + if(palindrome(s,i,j)) + return t[i][j] = 0; + + if(t[i][j]!=-1) + return t[i][j]; + + int temp,ans=INT_MAX; + int left,right; + for(int k = i;k<=j-1;k++) + { + if(t[i][k]!=-1) + { + left = t[i][k]; + } + else + { + left = solve(s,i,k); + } + if(t[k+1][j]!=-1) + { + right = t[k+1][j]; + } + else + { + right = solve(s,k+1,j); + } + + temp = 1+left+right; + ans = min(temp,ans); + } + return t[i][j] = ans; + + + } + int palindromicPartition(string str) + { + // code here + memset(t,-1,sizeof(t)); + int n = str.length(); + return solve(str,0,n-1); + } +}; + +//{ Driver Code Starts. + +int main(){ + int t; + cin>>t; + while(t--){ + string str; + cin>>str; + + Solution ob; + cout<