DP 28. Longest Palindromic Subsequence
Vložit
- čas přidán 4. 03. 2022
- Lecture Notes/C++/Java Codes: takeuforward.org/dynamic-prog...
Problem Link: bit.ly/3pvkqUd
Pre-req for this Series: • Re 1. Introduction to ...
a
Make sure to join our telegram group for discussions: linktr.ee/takeUforward
Full Playlist: • Striver's Dynamic Prog...
In this video, we solve the Longest Palindromic Subsequence, please watch the LCS Dp video before solving this.
If you have not yet checked our SDE sheet, you should definitely do it: takeuforward.org/interviews/s...
You can also get in touch with me at my social handles: linktr.ee/takeUforward
As soon as you explained the reversing part, it clicked. Amazing video as always. Thank you very much!!!😀
Same here bro 🤜
Same here
same man
same, Striver is goated
Tabulation code of LCS is very important guys...if you understand how values are getting filled in the table...you can solve this problem by yourself by thinking this problem in terms of lcs...it's the first problem of dp on strings which I have solved by myself. #Understood bhaiya❤️❤️
bro agr
s1 = "bbabcd"
s2 = "dcbabb"
dono me "bab" common h to vo largest common substring se solve hoga na ??
to bhaiya ne largest common subsequence q bola h ??
@@sdexstarlord @अभय बिष्ट here, we are supposed to find the palindrome subsequence whose length is as large as possible, right !!! Now the thing is are we care about a palindromic subsequence or palindromic substring. Just think & you can see you have to figure out the palindromic string which is nothing but the subsequence of the input string. If still, the thought process didn't strike ur mind let's take an example:-
s1 = ababmna
s2 = abadbefgha
So the answer according to your thought process should be just "aba" I.e of length 3 but common subsequence of s1 & s2 ababa which is palindrome & also largest. You were just thinking of the cases where you would get a palindromic string iff it is a common substring but it is not the case every time as you can see in the above example. Hope this might help🙂🙂
will you please send the link of lcs lectuare
@@sdexstarlord are vedya, every every substring is also subsequence, but the opposite is not always true.
@@sdexstarlord bhai kyuki apan ko subsequence chaiye na , isliye , agar largest commom substring bolta tab second wali approach se kar dete
Understood, thank you so much. It all seems to be summed up within 14 mins or so but the hard work and story behind coming up with logic such as this one, by "simply reversing the string" is always next level!
Amazing. Thanks a lot for bringing up this DP series and putting hard work for a good explanation. Now DP problem look easy 😀
Thankyou Striver🙏🏻❤
Before watching this video, I come up with two approaches by my own:
1) We can do this in one string recursively, starting point ==> f(n-1,0).
2) Find LCS of s and reverse(s).
**Approach 1 code c++:
class Solution {
public:
int f(int i, int j, string &s, int n, vector&dp){
if(i < 0 || j > n-1) return 0;
if(dp[i][j] != -1) return dp[i][j];
if(s[i] == s[j]) return dp[i][j] = 1 + f(i-1,j+1,s,n,dp);
else return dp[i][j] = max(f(i,j+1,s,n,dp), f(i-1,j,s,n,dp));
}
int longestPalindromeSubseq(string s) {
int n = s.length();
vectordp(n,vector(n,-1));
return f(n-1,0,s,n,dp);
}
};
**Approach 2 code c++:
class Solution {
public:
int longestCommonSubsequence(string s1, string s2) {
int n1 = s1.length(), n2 = s2.length();
vectordp(n1+1,vector(n2+1,0));
dp[0][0] = 0;
for(int i1 = 1; i1
I cant believe that after reaching to 29th video of this series , I can think DP solutions by myself and I can solve those questions, thnx alot striver bhaiya, you are doing great work
the moment u said char at start and char at end got the idea and able to solve the que. Thanks
I did it without watching explanation itself by the knowledge of previous videos truly thank u
Did what you said in LCS tutorial "For string it is either matching or not matching" and solved it using two pointer and dp.Thank You!!!
This was just simply amazing !!
UNDERSTOOD...!!!
Thank you striver for the video... :)
Understood....Completed (28/56). HALF WAY THROUGH🙌🙌
Hey Striver thanks man. Great explaination. I got the gist of how to approach n solve this question the moment you said reverse here 4:27
Understood! What a fantastic explanation as always!!
You are amazing. Really wonderful Explanation.
Absolutely brilliant.
understood bro, thanks a lot for making this question look so simple.
Understood, thank you for the awesome explanation
Understood. Thanks for the video!
Understood, sir. Thank you very much.
My goodness, who would have thought there can be such a thing which will lead to the ans of LPS...........Amazing Intuition Bhaiyaaaaaaaaaaaaa Hatssssss Offfffffff !!!
Thanks striver❣️
Thank you very much brother. Thanks a lot. Accha lagta hai jab sawal ho jata hai...
Understood! Thanks!
Amazing explanation , thanks Striver
Thanks striver for explaining the reverse logic.
Correct Problem link: www.codingninjas.com/codestudio/problems/longest-palindromic-subsequence_842787
thanks
gazab explanation bhai , understood just in a half video
thank you striver
Very nice explanation sir, Thank you!
fantastic approach! I always thought that you would have to cut the string into 2 parts and find the lcs in O(n^2 * k) lol
Understand Sir, Thank you very much
I did this question without studying recursion from gfg, tried to make head and tail out of it using debugger, that was a nightmare, now after studying recursion I can't describe how easy this is .
after watching your previous video, I can solve this question on the fly before watching this videooo.!
#UNDERSTOOD........Thank You So Much for this wonderful video.......🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
UNDERSTOOD!!!🔥🔥🔥🔥🔥🔥
#UNDERSTOOD LCS is amazing..we can solve a lot of using that logic only...Thanks
Problem Link : www.codingninjas.com/codestudio/problems/longest-palindromic-subsequence_842787?leftPanelTab=0
Understood, great explanation
Understood🔥🔥
Understood thank you so much
I came up with the solution without watching the video. Damn man, thanks !!!!!!
Mind blowing solution
That feeling of solving the DP question by self without watching the solution at all.
Thanks a lot, Striver. I was scared to touch the DP before :D but not anymore :) ❤
Superb Sir, Understood
Amazing. understood
Hey Sir!!
How do it print the longest palindromic substring.
This is so good ❤ now dp is easy because of you.. US ❤
Hey Striver! Can you please do a video on LC 139 . Word Break??
Amazing!!!
Understood 🙏
understood striver !!
understood bhaiya ❤
Understood Sir
we can also do it by using longest common subsequence where we will start the dp by 0 to len -1 of string then if the character matches we will recursively make the i and j move close both a unit step and if not we will thereforce use left right and when i crosses j we will return
understood Thanks raj😁
Thanks!
Understood Sir!
Understood!!!
UNDERSTOOD ...
gazabbbbbb yrrrr❤❤🔥🔥🙌🙌🙏🙏✨✨
Understood !!
Understood!
Halfway done 👍 🙌
Understood🌻
Just after writing the second string, my mind clicked.
Please update the link with LPS , its actually of LCS
understood!
Understood 🎉
UNDERSTOOD!
Understood.
Understood bro!!
"Solved without seeing the video " logic is improving day by day
understood bhaiya
UNDERSTOOD
so good
Understood 🙂🙂💚
understood very well, but what to do if I have to return the Longest Palindromic Substring and not the length
please update the problem link in the description :)
Understood ❤
Understood. 🤟🏻
Understood❤
Understood 😊
understood, did this by myself.
Understood Sir.
understood!!
I did this in the single String itself by recursive 2 pointer f(0, n -1). It is working great and superbb !! and also faster than the lcs(s, reverse(S)) method in this video😁😁😁😀
mind blown
understood sir🙏🙏❤️❤️
Mindblown bhai 🔥
❤❤ understood
Understood👍
Where I can get the java code for this problem sir I cannot find in the description notes
How could we print the longest pallindromic substring . I'm able to generate the correct dp array.I could get the max value also from dp array
this is the best dp series man Understood bhaiya ❤❤
Understood 🥰
the reversing method won't work for palindromic substring
Understood sir🙏❤🙇♂
Understood Sirr
Understood