From ff1f767a07116189c0fc6156df6b7372b3d37e89 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Sun, 28 Jan 2024 16:43:10 +0530 Subject: [PATCH 1/6] Create Tech-neophyte--cp.md --- .../Tech-neophyte--cp.md | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md 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..be282ee --- /dev/null +++ b/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md @@ -0,0 +1,51 @@ +## 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]; //to store palindromic status of each substring + for(int g=0;g Date: Sun, 28 Jan 2024 16:45:53 +0530 Subject: [PATCH 2/6] Update and rename Tech-neophyte--cp.md to Tech-neophyte--c.md --- .../{Tech-neophyte--cp.md => Tech-neophyte--c.md} | 4 ++++ 1 file changed, 4 insertions(+) rename Day-26/q1: Palindrome Partitioning II/{Tech-neophyte--cp.md => Tech-neophyte--c.md} (82%) diff --git a/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md b/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--c.md similarity index 82% rename from Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md rename to Day-26/q1: Palindrome Partitioning II/Tech-neophyte--c.md index be282ee..dcd50e5 100644 --- a/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md +++ b/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--c.md @@ -1,3 +1,7 @@ +## Approach +
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 { From a7dc04ef93437ea30ff8a236f33dc5ac9433363a Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Sun, 28 Jan 2024 16:48:02 +0530 Subject: [PATCH 3/6] Update and rename Tech-neophyte--c.md to Tech-neophyte--cp.md --- ...ch-neophyte--c.md => Tech-neophyte--cp.md} | 47 +++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) rename Day-26/q1: Palindrome Partitioning II/{Tech-neophyte--c.md => Tech-neophyte--cp.md} (58%) diff --git a/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--c.md b/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md similarity index 58% rename from Day-26/q1: Palindrome Partitioning II/Tech-neophyte--c.md rename to Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md index dcd50e5..bbc75b4 100644 --- a/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--c.md +++ b/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md @@ -24,7 +24,7 @@ public: } int minCut(string s) { int n=s.size(); - int check[n][n]; //to store palindromic status of each substring + int check[n][n]; for(int g=0;g Date: Sun, 28 Jan 2024 18:34:42 +0530 Subject: [PATCH 4/6] Update Tech-neophyte--cp.md --- Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md b/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md index bbc75b4..0e25ae6 100644 --- a/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md +++ b/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md @@ -1,3 +1,5 @@ +## Approach 1: Using recursion + ## Approach
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. From cd2a0badf87c5f9c3c095c38348ca5460a19c2a7 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Sun, 28 Jan 2024 18:35:22 +0530 Subject: [PATCH 5/6] Add another approach --- .../Tech-neophyte--cp.md | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md b/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md index 0e25ae6..6763a1f 100644 --- a/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md +++ b/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md @@ -1,5 +1,44 @@ ## 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. From 29982cc9f05d03628cd9a373f7c4b77dc4240b68 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Sun, 28 Jan 2024 18:36:40 +0530 Subject: [PATCH 6/6] Update Tech-neophyte--cp.md --- Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md b/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md index 6763a1f..a86c5f4 100644 --- a/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md +++ b/Day-26/q1: Palindrome Partitioning II/Tech-neophyte--cp.md @@ -39,7 +39,7 @@ public: } }; ``` -## Approach +## Approach 2: Using dp and memorisatiom
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.