diff --git a/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md b/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md new file mode 100644 index 0000000..a86c5f4 --- /dev/null +++ b/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md @@ -0,0 +1,137 @@ +## Approach 1: Using recursion +## cpp code +``` +class Recursion { +private: + int n; + bool isPalindrome(string& s, int l, int r) + { + while(l < r) + { + if(s[l++] != s[r--]) + { + return false; + } + } + return true; + } + int solve(string& s, int idx) + { + if(idx >= n) + { + return 0; + } + int minSteps = INT_MAX; + for(int k=idx; k Step 1: Take all substrings and check that it is palindrome or not and store in check matrix. +
Step 2: For each i, store all indexes such that s[i]....s[j] is a palindrome. +
Step 3: Now apply dp with memoisation to find minimum partitions. +## CPP code +``` +class Solution { +public: + vector adj[2005]; + int dp[2005]; + int help(int i,int n){ + if(i==n){ + return 0; + } + if(dp[i]!=-1){ + return dp[i]; + } + int ans=1e9; + for(auto x:adj[i]){ + int tmp=1+help(x+1,n); + ans=min(ans,tmp); + } + return dp[i]=ans; + } + int minCut(string s) { + int n=s.size(); + int check[n][n]; + for(int g=0;g