Longest Palindromic Subsequence Dynamic Programming Explained with Code

  Рет қаралды 35,789

Pepcoding

Pepcoding

Күн бұрын

Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we discuss the problem Longest Palindromic Subsequence using dynamic programming. In this problem,
1. You are given a string str.
2. You are required to print the length of longest palindromic subsequence of string str.
To submit this problem, click here: www.pepcoding....
For a better experience and more exercises, VISIT: www.pepcoding....
#dp #dynamicprogramming #palindrome
Have a look at our result: www.pepcoding....
Follow us on our FB page: / pepcoding
Follow us on Instagram: / pepcoding
Follow us on LinkedIn: / pepcoding-education

Пікірлер: 135
@Akshatved
@Akshatved 3 жыл бұрын
I have seen a lot of youtubers teach and even a lot of paid instructors teach. But I haven't seen anyone explaining DP problems so well. I hope you get all the success you deserve.
@abhishekranjan1094
@abhishekranjan1094 2 жыл бұрын
Super sir 🤩🤩🤩🤩🤩🤩🤩🤩🤩🤩
@SameerShinde-14051994
@SameerShinde-14051994 2 жыл бұрын
This entire video took 41 minutes from scratch to finish. In a real interview as well, this can be explained. Hats off on this explanation and clarity!!
@harshitha4886
@harshitha4886 Жыл бұрын
41 minutes of honest Content. Your approach to question not only helps to understand the solution to question but also develops the intuition to approach questions. Thanks a lot!!
@snehatandri8982
@snehatandri8982 3 жыл бұрын
Sir aap sachme gifted teacher hai... Teacher sirf vohi log bann sakte hai Jo student ke level pe jaake unko samjha paaye aur aap me ye talent hai. Thank you so much sir!
@Pepcoding
@Pepcoding 3 жыл бұрын
wow, this cheers me up. I am glad we at pepcoding could be of help to you. Keep learning. Also, recommend us to your juniors and peers, they may also benefit.
@snehatandri8982
@snehatandri8982 3 жыл бұрын
@@Pepcoding sure sir!
@adityaojha2701
@adityaojha2701 3 жыл бұрын
No other can explain this much better than u. Really appreciate your work.
@aniket7512
@aniket7512 4 жыл бұрын
U made my day sir! U not only explained this well but in general made me realize when to use a 2-D DP table! Thank u sir!😍 I should pay u my college fees......😁
@Pepcoding
@Pepcoding 4 жыл бұрын
Keep learning
@rushilahuja3369
@rushilahuja3369 3 жыл бұрын
The more videos I watch, the more I start respecting you sirji ! I like the way you explain things sirji ! Massive respect for you !! Hats off and thanks for the great explanation.
@Ballistic_Bytes
@Ballistic_Bytes 2 жыл бұрын
Ek bar mein samajh aa gaya itna clear explaination tha. Awesome video. Thanks for this
@guptapaaras
@guptapaaras 3 жыл бұрын
Sir after seeing the video of "count of palindromic subeqs". I managed to do this on my own. Thank you Sir for the hard work.
@rahulkaswan6315
@rahulkaswan6315 2 жыл бұрын
Great Explanation, I can bet that no one could explain this, better than you , after getting your explanation i never needed to see code , saved this for future reference, thanks Sumeet sir for such a wonderful video .
@aakashyadav6228
@aakashyadav6228 3 жыл бұрын
LPS of a string is LCS(string,reverse string) as simple as that.
@411_danishranjan9
@411_danishranjan9 3 жыл бұрын
bhai Gazab,
@rajdeepghosh5556
@rajdeepghosh5556 2 жыл бұрын
Really ? 😶😶😶
@jayeshshrivastava5424
@jayeshshrivastava5424 2 жыл бұрын
No it won't work for abcdefgdcba
@milindsharma2002
@milindsharma2002 3 жыл бұрын
Best channel ever for Dynamic Programming..........👍👍
@amitupadhyay6511
@amitupadhyay6511 2 жыл бұрын
Perfect, part one explanation. I used it to create a recursion with memorization and it worked like a charm .
@arpitgupta1790
@arpitgupta1790 3 жыл бұрын
Hat off to you for making such great and detailed videos.
@Pepcoding
@Pepcoding 3 жыл бұрын
Thankyou beta! If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms ) Keep learning and keep loving Pepcoding😊
@talentpicker
@talentpicker 3 жыл бұрын
The "else if" part in line no. 16 and 17 are not required. However, the explanation is really awesome. Hats off to you.
@HarivanshKripaVlogs
@HarivanshKripaVlogs 3 жыл бұрын
Your video are guarantee that i will definitely get the approach..!! Thank You Sir
@Pepcoding
@Pepcoding 3 жыл бұрын
Glad to know that you liked the content and thank you for appreciating. The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos. So, keep motivating, keep learning and keep loving Pepcoding😊
@ayushabhishekkumar6703
@ayushabhishekkumar6703 2 жыл бұрын
Wow!!! Brilliant explaination
@ujjvalcodes3629
@ujjvalcodes3629 2 жыл бұрын
there is a better approach using O(n) space, but I do appreciate your time and effort. Thanks 😃
@spicyedutainment1577
@spicyedutainment1577 9 ай бұрын
Really it's a very awesome video. Very well explaintaion sir
@paryt7696
@paryt7696 2 жыл бұрын
This video is best for learning part, but just to solve this question I have different approach, a string is given we make b string by reversing a and then find longest common subsequence between these two strings and we got the length of longest palindromic subsequence
@Pepcoding
@Pepcoding 2 жыл бұрын
For more detailed and curated content on DSA, Web Dev etc make sure to visit nados.pepcoding.com :)
@adityarathi3420
@adityarathi3420 2 жыл бұрын
🔥🔥🔥🔥
@akashmishra5553
@akashmishra5553 3 жыл бұрын
Thank you Sir, your videos are helping me a lot to prepare for my upcoming placements.
@Pepcoding
@Pepcoding 3 жыл бұрын
Glad to hear that. Keep learning, Keep growing and keep loving Pepcoding!😊
@amitjohn8845
@amitjohn8845 3 жыл бұрын
The best explanation so far !!
@Pepcoding
@Pepcoding 3 жыл бұрын
Glad it was helpful! and If you like our efforts, please upvote the comments written by the students about Pepcoding here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
@ashdavv
@ashdavv 2 жыл бұрын
Great way to explain the questions !!! Can you provide videos for other questions as well . I found this is the best channel to understand the questions
@Pepcoding
@Pepcoding 2 жыл бұрын
Bro, each and every questions are uploaded on nados.pepcoding.com You will find better experience, you can post your query on community tab and will find well organised content. Don't forget to subscribe our KZbin channel as well.
@sandeshkumarchandrakar1622
@sandeshkumarchandrakar1622 2 жыл бұрын
i did it by reversing the string s1 and then perform lcs on original s1 and reversed s1.
@gouravgoel2974
@gouravgoel2974 3 жыл бұрын
Sumit sir is our jeetu bhiaya
@SonuKhan-mp2yn
@SonuKhan-mp2yn 10 ай бұрын
Legendary Thank You................
@notinrange
@notinrange 3 жыл бұрын
Best explanation and your song sir
@cseengineering8582
@cseengineering8582 2 жыл бұрын
Explanation and song both are mast🤗
@Pepcoding
@Pepcoding 2 жыл бұрын
Glad you liked it. For better experience sign up on nados.io
@allwell8570
@allwell8570 3 жыл бұрын
Crystal clear explanation
@Pepcoding
@Pepcoding 3 жыл бұрын
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem. If you like our efforts, we request a review g.page/Pepcoding/review?rc You can subscribe to our channel here kzbin.infoabout?view_as=subscriber
@ananyaarya2465
@ananyaarya2465 3 жыл бұрын
Real DP starts from this video XD!!
@KundanKumar-im1gs
@KundanKumar-im1gs 3 жыл бұрын
thank you very much sir...really amazing explanation
@ashwinnema06
@ashwinnema06 2 жыл бұрын
Nice explanation sir.
@rahulranjan7567
@rahulranjan7567 3 жыл бұрын
Sir yeh question toh hum count palindromic substrings ke matrix se bhi karr sakte hai. Jahan bhi True aaya hai jis cell mein udhar ki (j-i)+1 dekhte jaayenge and last mein inka max nikal denge. Won't this work?
@411_danishranjan9
@411_danishranjan9 3 жыл бұрын
bhaiya bas yeh bata do, DP ki size humein kaise pata chalega ki dp[ str.length() + 1 ] ki banani h ya dp[ str.length() ]; yeh dp ki size mein plus one kab lagta h , aur kab nhi?
@arshadhussain9152
@arshadhussain9152 4 жыл бұрын
sir, did this question by lcs method. where string s1=input_string & s2=revserse(s1); Is it equally right?
@Pepcoding
@Pepcoding 4 жыл бұрын
yes
@rahulranjan7567
@rahulranjan7567 3 жыл бұрын
It's easier but Sumeet sir's approach is flexible. You can use it in almost all the questions which asks anything about substrings.
@kanishkanand1555
@kanishkanand1555 4 жыл бұрын
Bohat sundar samjhaya master ji
@Pepcoding
@Pepcoding 4 жыл бұрын
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem. If you like my efforts, I request a review g.page/Pepcoding/review?rc
@kanishkanand1555
@kanishkanand1555 4 жыл бұрын
@@Pepcoding done and thanks again for such quality content
@sasikiran2450
@sasikiran2450 4 жыл бұрын
Great explanation sir , maza aa gaya...
@Pepcoding
@Pepcoding 4 жыл бұрын
You can subscribe to our channel here kzbin.infoabout?view_as=subscriber And you can review us here g.page/Pepcoding/review?rc
@rishikeshmishra715
@rishikeshmishra715 4 жыл бұрын
Thank you Sumit Sir
@Pepcoding
@Pepcoding 4 жыл бұрын
Keep learning
@KingSlayr
@KingSlayr 3 жыл бұрын
we could have done this problem by finding longest common subsequence between given string and reverse of that string.
@Mercer80
@Mercer80 3 жыл бұрын
nice
@niranjanbs7549
@niranjanbs7549 3 жыл бұрын
Nice Thanks
@ankoor
@ankoor 3 жыл бұрын
Brilliant!
@iyashwantsaini
@iyashwantsaini 4 жыл бұрын
perfect explanation!
@Pepcoding
@Pepcoding 4 жыл бұрын
Glad it was helpful!
@nknidhi321
@nknidhi321 3 жыл бұрын
Beautiful explanation.. :)
@Pepcoding
@Pepcoding 3 жыл бұрын
Glad you liked it
@kumarakash5219
@kumarakash5219 2 жыл бұрын
Longest palindrome subsequence can also be solved by taking lcs of (string s,reverse of string s)
@jayeshshrivastava5424
@jayeshshrivastava5424 2 жыл бұрын
abcdefgdcba try to solve for this and your method won't work
@mrinmoyhalder7293
@mrinmoyhalder7293 2 жыл бұрын
@@jayeshshrivastava5424 bro it's working
@jayeshshrivastava5424
@jayeshshrivastava5424 2 жыл бұрын
@@mrinmoyhalder7293 yes bro, thanks for responding.
@Rahul-bl6rv
@Rahul-bl6rv 4 жыл бұрын
sir my question in java get TLE But the same code in C++ get accepted What will i do for it to make java function is faster ( like out of 30% Question this situation occurs )
@Pepcoding
@Pepcoding 4 жыл бұрын
I will post a video about performance issues of java and how they could be addressed.
@mickyman753
@mickyman753 3 жыл бұрын
@@Pepcoding sir can you share the link ,if you have made one?
@wecan2729
@wecan2729 2 жыл бұрын
we can simply reverse the string and check for lcs in both the string that is reverse and actuall string is that true?
@Pepcoding
@Pepcoding 2 жыл бұрын
For clearing all your doubts visit on nados.pepcodin.com
@RAHUL-gf3ft
@RAHUL-gf3ft 2 жыл бұрын
nhi for eg: take abcdfgdcba
@factswithai2
@factswithai2 3 жыл бұрын
east ho ya west sumeet sir is the best
@Pepcoding
@Pepcoding 3 жыл бұрын
, keep motivating, keep learning and keep loving Pepcoding😊
@jatinkumar4410
@jatinkumar4410 3 жыл бұрын
Awesome as usual...
@Pepcoding
@Pepcoding 3 жыл бұрын
Thanks buddy! If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )
@jatinkumar4410
@jatinkumar4410 3 жыл бұрын
@@Pepcoding sure sir. My pleasure.
@arvindsinha1566
@arvindsinha1566 3 жыл бұрын
you are good singer also :)
@Pepcoding
@Pepcoding 3 жыл бұрын
Keep learning, Keep growing and keep loving Pepcoding!😊
@tanishkumar6682
@tanishkumar6682 Жыл бұрын
understood
@KB-zg8ho
@KB-zg8ho 4 жыл бұрын
Sir in which question have you explained the gap method in detail?
@Pepcoding
@Pepcoding 4 жыл бұрын
2d arrays mei diagonal traversal
@NaveenSharma-pc5ej
@NaveenSharma-pc5ej 4 жыл бұрын
Sir please make vide making change problem of UVA 🙏
@Pepcoding
@Pepcoding 4 жыл бұрын
ok ji sure.
@abhinavgarg7654
@abhinavgarg7654 3 жыл бұрын
What is UVA?
@abhayyadav1468
@abhayyadav1468 4 жыл бұрын
Hello sir jo recently webinar hua h uska video youTube p dale h yaa nhi please daal dijiye?? ??
@Pepcoding
@Pepcoding 4 жыл бұрын
beta college webinars upload nahi karte.
@abhayyadav1468
@abhayyadav1468 4 жыл бұрын
Aacha sir thanks🌹
@abhayyadav1468
@abhayyadav1468 4 жыл бұрын
Kya hum baat kr skte hai Aap se??
@ayushgoel9584
@ayushgoel9584 4 жыл бұрын
sir bits mein ek check divisibility by 3 ki video private aa rhi hai,
@Pepcoding
@Pepcoding 4 жыл бұрын
usme error hai yar, usko theik karke kal dalunga.
@anjneykumarsingh4461
@anjneykumarsingh4461 4 жыл бұрын
Bhai negative admi h Tu, ek dialogue yd agya
@bismeetsingh352
@bismeetsingh352 3 жыл бұрын
Where to study gap strategy.
@manishkumarsingh4631
@manishkumarsingh4631 2 жыл бұрын
30/79 Done
@Pepcoding
@Pepcoding 2 жыл бұрын
Keep going.
@shachisharma8045
@shachisharma8045 3 жыл бұрын
Sir, we are taking LPS of abkccbc as 4. Why don't we consider a middle element and make it 5?
@Pepcoding
@Pepcoding 3 жыл бұрын
beta mere hisaab se to bccb hai, aap btaie isse bda kaun sa hai?
@shachisharma8045
@shachisharma8045 3 жыл бұрын
bckcb bhi toh palindrome hua sir?
@Pepcoding
@Pepcoding 3 жыл бұрын
aapne characters ka order change kar dia. subsequence mei ye allowed nahi hota
@shachisharma8045
@shachisharma8045 3 жыл бұрын
Okay.. yes sir... Thank you 😊
@ArcGaming07YT
@ArcGaming07YT 2 жыл бұрын
the solution is for longest palindromic substring but we have to find for subsequences
@democratcobra
@democratcobra 2 жыл бұрын
Sir app ko kese yeah sab pata chala sir..?
@vivekkumarsingh7172
@vivekkumarsingh7172 3 жыл бұрын
Too good
@nuamaaniqbal6373
@nuamaaniqbal6373 2 жыл бұрын
Best !!
@Pepcoding
@Pepcoding 2 жыл бұрын
Glad you love the explanation, for better experience and well organised content sign up on nados.io and start learning.
@cartube6219
@cartube6219 Жыл бұрын
Concept badaliye apne sir , isse aasan bhi hai recursive approch dp me
@Pepcoding
@Pepcoding Жыл бұрын
java ki recursion wali DP hackerrank pe test case pass nahi karti, stack bhar jati hai. Sirf tabulation wali he chalti hai. (If you want to check try sherlock and cost question on hackerrank to know why it is necessary for java programmers to do DP via tabulation) C++ mei dono chal jati hain.
@PankajKP
@PankajKP 4 жыл бұрын
Sir Interview prep k liye offcampus cp codechef 5 6 start zruri hai kya?
@anjneykumarsingh4461
@anjneykumarsingh4461 4 жыл бұрын
Bhai sir k famous dialogue h, zaroori toh duniya m bhut kch ni h, amazon microsoft jaana zaruri h , Tu bata space p jana jruri tha kya
@PankajKP
@PankajKP 4 жыл бұрын
@@anjneykumarsingh4461 bhai tum kuch or matlb nikal rhe ho. On campus to zruri nhi h ye to muje pata hai but offcampus resume shortlist nhi hota kahi bhi to kuch mediocre jaisa lgta hai har kisi k pas 6 star
@anjneykumarsingh4461
@anjneykumarsingh4461 4 жыл бұрын
Vhi toh bolra hu zaruri toh kch hota hi ni h, but tm zitna kroge utna cha
@PankajKP
@PankajKP 4 жыл бұрын
@@anjneykumarsingh4461 faang k liye ds algo zruri hai y to sbko pata hai ab bolo y bhi zruri nhi hai
@Pepcoding
@Pepcoding 4 жыл бұрын
beneficial hai bro, jaroori nahi
@vijaynaryansingh8401
@vijaynaryansingh8401 3 жыл бұрын
Sir khud se etna soch pana bahut msukil sa hai kaise soche sir previous pattern per based nhi tha ye but sir esme soche kaise
@Pepcoding
@Pepcoding 3 жыл бұрын
Beta, jab aise he solutions dekh dekh kr aur practise kr k 300-400 svaal kr loge, to apne aap thinking capability bd jayegi aur khud se soch paoge fr aap khud he approach.
@ashutoshjha5356
@ashutoshjha5356 3 жыл бұрын
sir, by this method we can get the length of all the substrings but not subsequence, because in longest palindromic substring you are using same technique.
@mehulmulchandani8126
@mehulmulchandani8126 3 жыл бұрын
Sir when to use 2D dp array?
@Pepcoding
@Pepcoding 3 жыл бұрын
jab question 2 parameter se define ho dp ka
@nehaagrawal670
@nehaagrawal670 3 жыл бұрын
How to write the substring here? the longest substring?
@AshutoshKumar-oo3hc
@AshutoshKumar-oo3hc 3 жыл бұрын
The pattern that I have notices so far is that => { subsequence == substring + you can leave the elements in-between but cannot mess the order }. To find the substring, maintain a 2-D dp array of boolean type and also have a global values of your start index and end index of your substring with global max_size of substring. Once you encounter a substring that has size greater than your global max_size, then update your start index and end index with new values of i and j. This way you can return the ans. But whenever we encounter the problem around subsequence we tend to use Integer [ ][ ]DP. The idea remains same but the matrix now has the changed meaning. Each box tend to store the maximum possible till that box. Printing the largest palindromic subsequence is not possible without actually performing additional tasks. I hope this helps anyone. Cheers !!!
@indranilchakraborty5949
@indranilchakraborty5949 3 жыл бұрын
💥💥🙏🙏🙏❤
@Pepcoding
@Pepcoding 3 жыл бұрын
😍🥰🙏🏼
@trading.helper-1997
@trading.helper-1997 4 жыл бұрын
Dislike kon maara be. Nahi chhodega usko.
@abhishekranjan1094
@abhishekranjan1094 2 жыл бұрын
sir..it is not working for "abccccdd" Output should be 7 but your 4 leetcode -409 😃😃😃 Check it and reply thanks 🙏🙏❤️😃
@Pepcoding
@Pepcoding 2 жыл бұрын
visit nados.pepcoding.com, post your doubts there community will help you out.
@RAHUL-gf3ft
@RAHUL-gf3ft 2 жыл бұрын
bhai 4 hi to hai "cccc"
Kluster Duo #настольныеигры #boardgames #игры #games #настолки #настольные_игры
00:47
Win This Dodgeball Game or DIE…
00:36
Alan Chikin Chow
Рет қаралды 45 МЛН
Как мы играем в игры 😂
00:20
МЯТНАЯ ФАНТА
Рет қаралды 3,4 МЛН
Longest Common Substring Dynamic Programming
27:22
Pepcoding
Рет қаралды 27 М.
Leetcode 5. Longest Palindromic Substring
23:52
Code with Alisha
Рет қаралды 47 М.
DP 31. Shortest Common Supersequence | DP on Strings
26:44
take U forward
Рет қаралды 162 М.
DP - 9:  Longest Palindrome Subsequence
30:29
Coding Simplified
Рет қаралды 10 М.
Kluster Duo #настольныеигры #boardgames #игры #games #настолки #настольные_игры
00:47