Тёмный
No video :(

DP 53. Palindrome Partitioning - II | Front Partition 🔥 

take U forward
Подписаться 651 тыс.
Просмотров 97 тыс.
50% 1

Опубликовано:

 

24 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 248   
@aayush5474
@aayush5474 2 года назад
How is Time complexity N^2? What about the extra loop for checking palindrome?
@takeUforward
@takeUforward 2 года назад
Oops missed it, my bad, you are correct.
@yuvi3398
@yuvi3398 2 года назад
@@takeUforward We can create another dp table in which dp[I][j] will tell whether substring s(I,j) is palindrome or not hence check function's time complexity reduces to O(1) and overall time complexity to O(n^2)
@takeUforward
@takeUforward 2 года назад
@@yuvi3398 yes pre computations will help
@himanshuparihar7423
@himanshuparihar7423 2 года назад
@@takeUforward Sir I submitted the same question on leetcode but it's giving TLE even in the tabulation part it's giving TLE. But when I used recursive way to check Palindrome... it got accepted....can you explain why recursive way accepted but the iterative method is giving TLE..
@harshdasila6680
@harshdasila6680 2 года назад
@@himanshuparihar7423 did you find any solution for that bro
@abhishekverma7604
@abhishekverma7604 Год назад
its a classical LPS variation..longest the palindromic substring partioned,minimum are the cuts... it was asked by meta recently in our on campus drive...
@maceback6622
@maceback6622 Год назад
We can find the longest palindromic substring but it is not sufficient to get the minimum cuts, because we have to identify other smaller palindromic substrings as well. Correct me if I'm wrong.
@rohitraj5832
@rohitraj5832 2 года назад
at 20:08.TC shoyuld be N^3 bcz everytime we are checking for palindrome also. it will also take O(n) time in worst case
@7akon
@7akon 6 месяцев назад
This can be boiled down to n^2. Check the parameters in the recursive function. Where are we using n? You can simply compute it to be str.length(). So the recursive parameters are simply: fn(string, start). And another linear factor introduced by the palindrome check. So n^2. The iterative code in this video is n^3 but the recursive code will be bounded by O(n^2).
@adrijsharma8404
@adrijsharma8404 Год назад
I think the time complexity is O(N^3). We have the recursion stack taking O(N). Inside each resusion step we have the j loop [O(n)] and inside each j loop we have the isPalindrome function which is O(n) again. n * n * n = n^3.
@balakrishnanr648
@balakrishnanr648 Год назад
How in leeetcode 1e3 contraint runs on O(N^3)
@iamnoob7593
@iamnoob7593 7 месяцев назад
yes u r right ,
@deepanshurohilla2114
@deepanshurohilla2114 Год назад
14:42 That was epic 😂😂😂😂
@harshkankariya4423
@harshkankariya4423 3 месяца назад
its tc wil be O(n^3) because we r chwcking for valid palindrome also
@stith_pragya
@stith_pragya 7 месяцев назад
UNDERSTOOD.......Thank You So Much for this wonderful video...........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@dawarvaibhav
@dawarvaibhav Год назад
Was able to solve on my own after your previous videos. Well taught as always! :)
@cacklouncle
@cacklouncle Год назад
Base case i == n ki jagah aisa lelo ki starting index se end tak puri string palindrome hai toh return 0. Aise last mein -1 bhi nahin karna padega aur time bhi kaafi bachega jaise test case mein bahut badi string hai jo already palindrome hai, toh recursive calls jayengi hi nahin straightaway 0 ans
@sayansahu4594
@sayansahu4594 2 года назад
Pass the string by reference to the Palindrome function to avoid getting a TLE
@JE_Dinesh
@JE_Dinesh 2 года назад
But why? What difference is it making? auxiliary space decreases right?
@sauravlogsvio1891
@sauravlogsvio1891 2 года назад
@@JE_Dinesh class Solution { public: bool isPalindrome(string& s, int start, int end) { while(start = j) return 0; if(cache[i] != -1) return cache[i]; if(isPalindrome(s, i, j)) return 0; int minCuts = INT_MAX; // Try all possible cuts between i and j for(int k=i; k
@JE_Dinesh
@JE_Dinesh 2 года назад
@@sauravlogsvio1891 i don't want solution code, i need explanation to it.
@sauravlogsvio1891
@sauravlogsvio1891 2 года назад
@@JE_Dinesh Pass by reference will be faster as you don't have to create and copy the whole string millions of times in the function call.. which takes memory as well as the time it takes to create as well. So, it's recommended to just pass a reference to it.
@JE_Dinesh
@JE_Dinesh 2 года назад
@@sauravlogsvio1891 thanks a lot bhai🤩
@ujjalsaha428
@ujjalsaha428 2 года назад
As always "understood" ❤️, just got confused on the Time Complexity Part 😵!
@lakshitjuneja7530
@lakshitjuneja7530 Год назад
when you did it was a medium question now this is a hard question may b test cases got more strict that's why this solution is giving TLE in large input
@saranshpathak3795
@saranshpathak3795 2 года назад
Its quite similar to climbing stairs ,just with palindromic twist...so smooth explanation by striver.
@priyanshkumar_iitd
@priyanshkumar_iitd Месяц назад
I agree!!!
@yugal8627
@yugal8627 11 месяцев назад
I had watched you Palindrome Partitioning-1 video and I had able to solve this by my own..😃
@prasunroy2802
@prasunroy2802 Год назад
Leetcode isn't accepting all test cases for java. We will have to store the possible palindromes in form of a 2d matrix and access this matrix when checking for the isPalin condition in the recurrence.
@shyamsundersrd
@shyamsundersrd 9 дней назад
Thanks Alot master. Understood. 👏
@1tav0
@1tav0 Год назад
This was so smooth, us
@solitaryassasin
@solitaryassasin Год назад
This can be solved by the traditional MCM pattern too. Partition at a place and check for min partitions on left and right.
@anishmangal9072
@anishmangal9072 Год назад
But Time complexity there will be O(N*N*(N+N)) that is equal to O(n3) -->
@solitaryassasin
@solitaryassasin Год назад
@@anishmangal9072 Here also it's N³
@swetanksrivastava3159
@swetanksrivastava3159 Год назад
Shouldn't the time complexity be O(N*N*N*N) i.e one for the i loop, one for the j loop, one for the k loop ,and one for the palindrome checking. Although, it can be optimised to O(N*N*N) by palindrome precomputation....please correct me if I am wrong.....Also I am seriously confused as to when we should apply front partitioning and traditional mcm approach respectively...
@sarthakgupta290
@sarthakgupta290 10 месяцев назад
It will be O(N^3) and will time out on LeetCode #132
@user-do1eq7tl3e
@user-do1eq7tl3e 2 месяца назад
can u explain a bit more ? @solitaryassasin , we will try to put partition at each point , and go in left and right sub parts , if its a substring then we return 0 , but how will we combine subparts ? I am confused . pseudo code will be helpful
@SelvaArumugam-xq5mf
@SelvaArumugam-xq5mf 7 месяцев назад
I had been following dp after completing yours recursion series and I remeber solving this now i solved this on my own before seing the vedio it was kindoff a cake walk without any errors
@ris_colid325
@ris_colid325 11 месяцев назад
What are the particular features of this problem over MCM problem that allow using this front partition approach here ?,because it was not possible for MCM problem .There we had the O(N^3) solution there considering all partitions and then solving both left and right sides of the partition as separate subproblems.
@AkshatMaheshwari-wm1yg
@AkshatMaheshwari-wm1yg 6 месяцев назад
same question
@sathvikmalgikar2842
@sathvikmalgikar2842 10 месяцев назад
sir you are great thank you so much for helping students with this gold level content. very easy to grasp and fun to solve !
@rishabhgupta9846
@rishabhgupta9846 Год назад
understood ,able to solve by myself
@MOHDSALMAN-sj2zu
@MOHDSALMAN-sj2zu 2 года назад
Those who are getting TLE. Try to memorize the palindrome check too. Take a reference of below code snipped: private int[][] dpPalin; private boolean isPalindrome(String s, int i, int j) { if(i>=j) return true; if(dpPalin[i][j] != 0) return dpPalin[i][j] == 1; else dpPalin[i][j] = (s.charAt(i) == s.charAt(j) && isPalindrome(s, i+1, j-1)) ? 1 : -1; return dpPalin[i][j] == 1; }
@prashudeshmukh7902
@prashudeshmukh7902 Год назад
still getting tle
@devanshbatra308
@devanshbatra308 Год назад
@@prashudeshmukh7902 try putting & before the "s" in the isPalindrome() function.
@piku9712
@piku9712 Месяц назад
Amazing. Thanks for this!!
@cinime
@cinime 2 года назад
Understood!! Thank you soooooo much as always!!!
@studynewthings1727
@studynewthings1727 4 месяца назад
Understood
@shashankvashishtha4454
@shashankvashishtha4454 Месяц назад
understood
@undefined_exception_0
@undefined_exception_0 Год назад
Thanks Striver for another amazing video #understood Optimized O(N^2) approach int minCuts(string& str) { int n = str.size(); // precomputing every substring is palindrome or not vector isPalindrome(n , vector (n , false)); for(int i=n-1 ; i>=0 ; i--){ for(int j=i ; j
@lakshaysharma6550
@lakshaysharma6550 2 года назад
UNDERSTOOD!!!
@Hrushi_2000
@Hrushi_2000 11 месяцев назад
Understood. Thankyou Sir
@shantipriya370
@shantipriya370 7 месяцев назад
wonderful explanation
@aryanraj4088
@aryanraj4088 2 года назад
Great Explanation ! Thank you for the video !! This solution is giving TLE on LeetCode !!
@kunjthakkar8725
@kunjthakkar8725 2 года назад
you have to pass string as pass by reference.
@vibhanshugarg3603
@vibhanshugarg3603 2 года назад
class Solution { public: bool isPalin(int i, int j, string& s){ while(i
@surajbhardwaj2599
@surajbhardwaj2599 Год назад
@@kunjthakkar8725 thanks bro!
@lakshyabansal2542
@lakshyabansal2542 Год назад
Understood Thanks man!
@ankitpandey7262
@ankitpandey7262 2 года назад
Understood ✌
@ratinderpalsingh5909
@ratinderpalsingh5909 Год назад
Understood, sir. Thank you very much.
@samyakjain7422
@samyakjain7422 2 года назад
understood!!!!
@AyushKumar-ty4cl
@AyushKumar-ty4cl Год назад
What is the time complexity of memoization & tabulation solutions?? Is it O(n^3) ??
@shivamkumar5857
@shivamkumar5857 Год назад
yes
@ganeshkamath89
@ganeshkamath89 2 года назад
Thanks Striver. Understood.
@anshulgoel1940
@anshulgoel1940 Год назад
How to evaluate where to use MCM and where to use front partitioning. Somehow I have a hunch we can solve all the MCM problems using front partitioning. Please suggest.
@user-do1eq7tl3e
@user-do1eq7tl3e 2 месяца назад
yes , please someone explain , I also have same query .
@tikshavarshney213
@tikshavarshney213 2 года назад
Understood !
@prathmeshadsod629
@prathmeshadsod629 2 года назад
🤩u made dp so simple bro Thanks ... Can you make video on Travelling Salesperrson using DP
@yeswanthh5068
@yeswanthh5068 2 года назад
Understood 🙂
@arihantjammar8888
@arihantjammar8888 10 месяцев назад
Understood 😊
@csea_37_shayoribhowmick53
@csea_37_shayoribhowmick53 10 месяцев назад
Thank you so much
@nimmalavishnu3044
@nimmalavishnu3044 2 года назад
time complexity for tabulation solution o(n3)
@MOHDSALMAN-sj2zu
@MOHDSALMAN-sj2zu 2 года назад
It will be O(n^2) if you precompute palindrome check too.
@JE_Dinesh
@JE_Dinesh 2 года назад
@@MOHDSALMAN-sj2zu Otherwise it will be 0(n4) right? if we consider a 1 single palindrome string then for 1st forloop it will be n 2nd for loop n and for ispalindrome function will be n2 right(if i=0 then for each j from 0 to n we have to do palindrome check)?
@nanda_8
@nanda_8 2 года назад
@@MOHDSALMAN-sj2zu even If we precompute, That precomputation will end up taking n^3
@iamnoob7593
@iamnoob7593 7 месяцев назад
US striver
@harshdasila6680
@harshdasila6680 2 года назад
Leetcode giving TLE on all 3 solutions
@VaibhavSharma-mm3wv
@VaibhavSharma-mm3wv 2 года назад
Memoization passed
@rechinraj111
@rechinraj111 2 года назад
@@VaibhavSharma-mm3wv can u plz paste ur solution here? Actually its giving TLE in leetcode.
@rechinraj111
@rechinraj111 2 года назад
finally it worked. Just changed isPalindrome() method from iterative to recursive. Now in leetcode it passed.
@optimusprime5040
@optimusprime5040 2 года назад
Memoize both main function and palindrome checking function XD
@vibhanshugarg3603
@vibhanshugarg3603 2 года назад
passing by reference worked well!
@akashsahu2571
@akashsahu2571 Год назад
yes
@ayankhan-xh8zt
@ayankhan-xh8zt 7 месяцев назад
understood sir
@NARUTOUZUMAKI-bk4nx
@NARUTOUZUMAKI-bk4nx 8 месяцев назад
understood🙏
@LowkeyCoder
@LowkeyCoder 8 месяцев назад
"undersstood"
@cameye9
@cameye9 6 месяцев назад
Sir what about timeComplexity of checking isPalindrome is O(n) not Including in total TimeComplexity.
@AmarjeetKumar-to9ub
@AmarjeetKumar-to9ub Год назад
Thank You :)
@ashutoshgupta4516
@ashutoshgupta4516 Год назад
Tried solving it in Leetcode, found TLE with a long string with a's and 2 b's in the middle, is anyone else seeing the same error as well?
@princechaurasia7624
@princechaurasia7624 Год назад
yes
@mathematics7746
@mathematics7746 Год назад
@@princechaurasia7624 pass by reference to avoid tle
@ayushidalal5488
@ayushidalal5488 Год назад
@@mathematics7746 It worked. Thanks!!
@mathematics7746
@mathematics7746 Год назад
@@ayushidalal5488 wlcm
@Harshit126
@Harshit126 2 года назад
Understood, thanks
@Hello-ro6hr
@Hello-ro6hr 2 года назад
UNDERSTOOD ...
@parshchoradia9909
@parshchoradia9909 Год назад
Understood Sir!
@aditithakur6226
@aditithakur6226 Год назад
Understood Sir :)
@priyanshkumar_iitd
@priyanshkumar_iitd Месяц назад
Tabulation code for leetcode : class Solution { private: bool isPalindrome(string &s, int i, int j){ while(i < j){ if(s[i] != s[j]) return false; i++; j--; } return true; } public: int minCut(string s) { int n= s.length(); vector dp(n + 1, 0); for(int i = n - 1; i >= 0; i--){ int minCuts = INT_MAX; for(int j = i; j < n; j++){ if(isPalindrome(s, i, j)){ int cuts = 1 + dp[j + 1]; minCuts = min(minCuts, cuts); } } dp[i] = minCuts; } return dp[0] - 1; } };
@harshvardhansingh7863
@harshvardhansingh7863 2 года назад
thank you sir
@giraffe4375
@giraffe4375 2 года назад
Thanks striver
@srikanthbankuru9557
@srikanthbankuru9557 Год назад
understood dude :)
@mathematics7746
@mathematics7746 Год назад
can we optimized its space to only two variable????
@adityadixit7973
@adityadixit7973 9 месяцев назад
understood😁😁
@maheshjindal2622
@maheshjindal2622 Год назад
Understoooood
@priyanshvatsal9791
@priyanshvatsal9791 Год назад
Understood 🥰
@manasranjanmahapatra3729
@manasranjanmahapatra3729 Год назад
Understood💯
@suhaanbhandary4009
@suhaanbhandary4009 Год назад
Understood!!
@bhavya8608
@bhavya8608 Год назад
understoood!!!!
@anshumangupta1001
@anshumangupta1001 Год назад
Understood.
@arsyaswanth5
@arsyaswanth5 2 года назад
Understood sir!!!!
@siddharth794
@siddharth794 2 года назад
Why can't we go till n-1 to remove the extra partition at last
@sathyakrishnanthirunavukka7908
@sathyakrishnanthirunavukka7908 11 месяцев назад
US
@abhayrai1724
@abhayrai1724 Год назад
understood 😁
@I_Anupam_Pandey
@I_Anupam_Pandey Год назад
understand
@codingp110
@codingp110 3 месяца назад
US!
@divyatejaswinivengada6368
@divyatejaswinivengada6368 11 месяцев назад
US!!
@sameersahu4569
@sameersahu4569 2 года назад
Understood!!!
@475_nehakaushik5
@475_nehakaushik5 2 года назад
understoood
@subodhkasaudhaniitk796
@subodhkasaudhaniitk796 Год назад
Understood :)))
@vxbsjisjjdjjdn
@vxbsjisjjdjjdn 6 месяцев назад
I tried almost same approach but giving wrong answer, can anyone tell what is wrong in my code? CODE: class Solution: def minCut(self, s: str) -> int: def is_palindrome(sub): return sub == sub[::-1] def f(i, prev): if i == n: return 0 take = float('inf') notTake = f(i + 1, prev) if is_palindrome(s[prev:i + 1]): take = 1 + f(i + 1, i + 1) return min(take, notTake) n = len(s) return f(0, 0)
@Lucifer0872
@Lucifer0872 2 года назад
❤️❤️❤️❤️
@AkshatMaheshwari-wm1yg
@AkshatMaheshwari-wm1yg 6 месяцев назад
Can anyone tell me why are we using front partitioning here and not normal partitioning. Reply is highly appreciated!
@user-gu8hk3ub9f
@user-gu8hk3ub9f 2 месяца назад
i think we can use previous approach but that will give mle thats why we move to dp
@brokegod5871
@brokegod5871 24 дня назад
This approach will fail in leetcode because of the fact that this logic makes a cut when it finds a fresh palindrome, it doesn't maintain the flow if a palindrome is ongoing. For example, if you have "cdd", by his logic you will get answer as 2 because the cut first happens at c, then cd is invalid so goes to d in which another cut happens for one d, and then the last cut at last d which is extra for which we do minus one at the end but the answer is actually just one cut because c and "dd" itself is a palindrome, but his code cuts "dd" into two separate d's. You need to first precompute the presence of all possible palindromes and then the code is the same but this time we check from the precomputed array if the string being examined is already a palindrome or not. This is the memoised code, you can tabulate it easily. class Solution { public: bool isPalindrome(string &s, int i, int j, vector &dp) { if(i>=j) return true; if(dp[i][j]!=-1) return dp[i][j]; if(s[i]==s[j]) return dp[i][j] = isPalindrome(s, i+1, j-1, dp); else return dp[i][j] = false; } int solve(int i, int n, string &s, vector &dp, vector &dp2) { if(i==n) return 0; if(dp2[i]!=-1) return dp2[i]; string temp=""; int ans = 1e9; for(int j=i;j
@rayyansyed2998
@rayyansyed2998 10 месяцев назад
"us"
@rohalkurup1350
@rohalkurup1350 2 года назад
Recursive solution with memoization accepted in leetcode , PSB code class Solution { public int minCut(String s) { int dp[] = new int[s.length()]; Arrays.fill(dp,-1); return solve(0,s.length(),s,dp); } public int solve(int i,int n,String s,int []dp) { if(i==n || isPalindrome(s,i,n-1)) return 0; if(dp[i]!=-1) return dp[i]; int minCost = 9999; for(int j=i;j
@harshjain8596
@harshjain8596 Год назад
UnderStood - Below is the java code with pallindrome array public int minCut(String s) { int n = s.length(); boolean[][] dp = new boolean[n][n]; for(int jump=0;jump
@KartikRai-YrIDDCompSciEngg
@KartikRai-YrIDDCompSciEngg Год назад
Giving TLE on leetcode
@harishseepathi9459
@harishseepathi9459 Год назад
striver getting tle in leetcode
@meetsoni1938
@meetsoni1938 2 года назад
US 🔥🔥
@sudarshanwadajkar8323
@sudarshanwadajkar8323 Год назад
us
@AB-tp9eg
@AB-tp9eg 2 года назад
"US"
@piyushgaur6975
@piyushgaur6975 Год назад
Why Gap Strategy is giving TLE on this question?? //Gap Startegy ----> bool isPalindromic(string &s, int i, int j){ while(i
@051-avnee4
@051-avnee4 Год назад
US :)
@tusharnain6652
@tusharnain6652 Год назад
// N^2 time and N^2 space solution C++ void isPalinUtility(const string &s, vector &isPalin) { int n = s.size(); for (int i = n - 1; i >= 0; i--) { for (int j = i; j < n; j++) { if (i == j) // for single character //base case ya' know isPalin[i][j] = true; else if (i + 1 == j) // for 2 size string //kinda like base case isPalin[i][j] = s[i] == s[j] ? true : false; else { if (s[i] == s[j]) isPalin[i][j] = isPalin[i + 1][j - 1]; else isPalin[i][j] = false; } } } } int palindromicPartition(string str) { int n = str.size(); vector dp(n + 1); dp[n] = 0; vector isPalin(n, vector(n, false)); isPalinUtility(str, isPalin); for (int i = n - 1; i >= 0; i--) { int min_result = INT_MAX; for (int k = i; k < n; k++) { if (isPalin[i][k]) { int temp = 1 + dp[k + 1]; if (temp < min_result) min_result = temp; } } dp[i] = min_result; } return dp[0] - 1; }
@sakshimishra1491
@sakshimishra1491 Год назад
Use this updated code for java as test cases have been updated on leetcode and above memoization solution gives tle: class Solution { public int minCut(String s) { int[] cut = new int[s.length()]; boolean[][] p = new boolean[s.length()][s.length()]; for (int i = 0; i < s.length(); i++) { int minCut = i; for (int j = 0; j
@sanyamwalia217
@sanyamwalia217 Год назад
#include bool ispalin(string &s) { int n=s.length(); for(int i=0;i
@MicahJosephBIT
@MicahJosephBIT 2 года назад
In leetcode when I am trying to submit it's giving me Memory Limit Exceeded. What to do?
@light-ln3hy
@light-ln3hy 2 года назад
Pass your string via reference....use '&' operator
@gouravshaw8324
@gouravshaw8324 Год назад
whats wrng with this code giving WA in LC class Solution { public: bool isPalin(int i, int j, string s){ while(i>j){ if(s[i] != s[j]){ return false; } i++; j--; } return true; } int f(int i, int n, string str){ if(i == n) return 0; int min_cost = INT_MAX; for(int j = i;j
@priyankaram4353
@priyankaram4353 Год назад
c++ solution working on leetcode int t[2001][2001]; bool stDp[2001][2001]; int solve(string st, int i, int j){ if(i>=j) return t[i][j]=0; if(t[i][j]!=-1) return t[i][j]; if(stDp[i][j]) return t[i][j] = 0; int ans = INT_MAX; for(int k=i; k
Далее
Simple Flower Syrup @SpicyMoustache
00:32
Просмотров 2,4 млн
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Palindrome Partitioning - Backtracking - Leetcode 131
10:24
36  Palindrome Partitioning Recursive
26:35
Просмотров 191 тыс.
Simple Flower Syrup @SpicyMoustache
00:32
Просмотров 2,4 млн