Longest Palindromic Subsequence Dynamic Programming Explained with Code

Поділитися
Вставка
  • Опубліковано 25 сер 2024
  • 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 роки тому +22

    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.

  • @SameerShinde-14051994
    @SameerShinde-14051994 2 роки тому +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!!

  • @snehatandri8982
    @snehatandri8982 3 роки тому +2

    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 роки тому +1

      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!

  • @aniket7512
    @aniket7512 3 роки тому +19

    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......😁

  • @aakashyadav6228
    @aakashyadav6228 3 роки тому +16

    LPS of a string is LCS(string,reverse string) as simple as that.

  • @harshitha4886
    @harshitha4886 Рік тому +1

    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!!

  • @adityaojha2701
    @adityaojha2701 3 роки тому +3

    No other can explain this much better than u. Really appreciate your work.

  • @rushilahuja3369
    @rushilahuja3369 3 роки тому +1

    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.

  • @guptapaaras
    @guptapaaras 3 роки тому +1

    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.

  • @Ballistic_Bytes
    @Ballistic_Bytes 2 роки тому

    Ek bar mein samajh aa gaya itna clear explaination tha. Awesome video. Thanks for this

  • @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 .

  • @arpitgupta1790
    @arpitgupta1790 3 роки тому +7

    Hat off to you for making such great and detailed videos.

    • @Pepcoding
      @Pepcoding  3 роки тому +1

      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😊

  • @milindsharma2002
    @milindsharma2002 2 роки тому

    Best channel ever for Dynamic Programming..........👍👍

  • @ayushabhishekkumar6703
    @ayushabhishekkumar6703 2 роки тому +1

    Wow!!! Brilliant explaination

  • @talentpicker
    @talentpicker 2 роки тому

    The "else if" part in line no. 16 and 17 are not required. However, the explanation is really awesome. Hats off to you.

  • @amitupadhyay6511
    @amitupadhyay6511 2 роки тому

    Perfect, part one explanation. I used it to create a recursion with memorization and it worked like a charm .

  • @spicyedutainment1577
    @spicyedutainment1577 8 місяців тому

    Really it's a very awesome video.
    Very well explaintaion sir

  • @HarivanshKripaVlogs
    @HarivanshKripaVlogs 3 роки тому +1

    Your video are guarantee that i will definitely get the approach..!!
    Thank You Sir

    • @Pepcoding
      @Pepcoding  3 роки тому +1

      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😊

  • @gouravgoel2974
    @gouravgoel2974 2 роки тому +1

    Sumit sir is our jeetu bhiaya

  • @sandeshkumarchandrakar1622
    @sandeshkumarchandrakar1622 2 роки тому +2

    i did it by reversing the string s1 and then perform lcs on original s1 and reversed s1.

  • @ananyaarya2465
    @ananyaarya2465 3 роки тому +3

    Real DP starts from this video XD!!

  • @ujjvalcodes3629
    @ujjvalcodes3629 2 роки тому

    there is a better approach using O(n) space, but I do appreciate your time and effort. Thanks 😃

  • @akashmishra5553
    @akashmishra5553 3 роки тому +1

    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!😊

  • @SonuKhan-mp2yn
    @SonuKhan-mp2yn 9 місяців тому

    Legendary Thank You................

  • @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 роки тому

      🔥🔥🔥🔥

  • @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 UA-cam channel as well.

  • @amitjohn8845
    @amitjohn8845 3 роки тому +1

    The best explanation so far !!

    • @Pepcoding
      @Pepcoding  3 роки тому +1

      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 )

  • @notinrange
    @notinrange 3 роки тому +1

    Best explanation and your song sir

  • @cseengineering8582
    @cseengineering8582 2 роки тому +1

    Explanation and song both are mast🤗

    • @Pepcoding
      @Pepcoding  2 роки тому +1

      Glad you liked it.
      For better experience sign up on nados.io

  • @allwell8570
    @allwell8570 3 роки тому +1

    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
      ua-cam.com/users/Pepcodingabout?view_as=subscriber

  • @factswithai2
    @factswithai2 3 роки тому +1

    east ho ya west sumeet sir is the best

    • @Pepcoding
      @Pepcoding  3 роки тому

      , keep motivating, keep learning and keep loving Pepcoding😊

  • @KundanKumar-im1gs
    @KundanKumar-im1gs 3 роки тому

    thank you very much sir...really amazing explanation

  • @ashwinnema06
    @ashwinnema06 2 роки тому

    Nice explanation sir.

  • @sasikiran2450
    @sasikiran2450 3 роки тому

    Great explanation sir , maza aa gaya...

    • @Pepcoding
      @Pepcoding  3 роки тому

      You can subscribe to our channel here
      ua-cam.com/users/Pepcodingabout?view_as=subscriber
      And you can review us here
      g.page/Pepcoding/review?rc

  • @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?

  • @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

  • @ankoor
    @ankoor 2 роки тому

    Brilliant!

  • @KingSlayr
    @KingSlayr 3 роки тому +2

    we could have done this problem by finding longest common subsequence between given string and reverse of that string.

  • @tanishkumar6682
    @tanishkumar6682 Рік тому

    understood

  • @rishikeshmishra715
    @rishikeshmishra715 3 роки тому

    Thank you Sumit Sir

  • @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.

  • @iyashwantsaini
    @iyashwantsaini 3 роки тому

    perfect explanation!

  • @niranjanbs7549
    @niranjanbs7549 3 роки тому

    Nice Thanks

  • @arshadhussain9152
    @arshadhussain9152 3 роки тому +2

    sir, did this question by lcs method. where string s1=input_string & s2=revserse(s1);
    Is it equally right?

    • @Pepcoding
      @Pepcoding  3 роки тому

      yes

    • @rahulranjan7567
      @rahulranjan7567 3 роки тому +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.

  • @kumarakash5219
    @kumarakash5219 2 роки тому +1

    Longest palindrome subsequence can also be solved by taking lcs of (string s,reverse of string s)

  • @manishkumarsingh4631
    @manishkumarsingh4631 2 роки тому

    30/79 Done

  • @democratcobra
    @democratcobra Рік тому +1

    Sir app ko kese yeah sab pata chala sir..?

  • @arvindsinha1566
    @arvindsinha1566 3 роки тому +1

    you are good singer also :)

    • @Pepcoding
      @Pepcoding  3 роки тому

      Keep learning, Keep growing and keep loving Pepcoding!😊

  • @ArcGaming07YT
    @ArcGaming07YT Рік тому

    the solution is for longest palindromic substring but we have to find for subsequences

  • @nknidhi321
    @nknidhi321 3 роки тому +2

    Beautiful explanation.. :)

  • @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.

  • @Rahul-bl6rv
    @Rahul-bl6rv 4 роки тому +1

    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 роки тому +3

      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?

  • @bismeetsingh352
    @bismeetsingh352 3 роки тому

    Where to study gap strategy.

  • @ayushgoel9584
    @ayushgoel9584 4 роки тому +1

    sir bits mein ek check divisibility by 3 ki video private aa rhi hai,

    • @Pepcoding
      @Pepcoding  4 роки тому +2

      usme error hai yar, usko theik karke kal dalunga.

    • @anjneykumarsingh4461
      @anjneykumarsingh4461 4 роки тому

      Bhai negative admi h Tu, ek dialogue yd agya

  • @vivekkumarsingh7172
    @vivekkumarsingh7172 2 роки тому

    Too good

  • @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

  • @KB-zg8ho
    @KB-zg8ho 3 роки тому

    Sir in which question have you explained the gap method in detail?

    • @Pepcoding
      @Pepcoding  3 роки тому

      2d arrays mei diagonal traversal

  • @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.

  • @nehaagrawal670
    @nehaagrawal670 3 роки тому

    How to write the substring here? the longest substring?

    • @AshutoshKumar-oo3hc
      @AshutoshKumar-oo3hc 3 роки тому +1

      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 !!!

  • @NaveenSharma-pc5ej
    @NaveenSharma-pc5ej 4 роки тому

    Sir please make vide making change problem of UVA 🙏

  • @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

  • @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 роки тому +1

      aapne characters ka order change kar dia. subsequence mei ye allowed nahi hota

    • @shachisharma8045
      @shachisharma8045 3 роки тому

      Okay.. yes sir... Thank you 😊

  • @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.

  • @abhayyadav1468
    @abhayyadav1468 4 роки тому

    Hello sir jo recently webinar hua h uska video youTube p dale h yaa nhi please daal dijiye?? ??

  • @indranilchakraborty5949
    @indranilchakraborty5949 3 роки тому +1

    💥💥🙏🙏🙏❤

  • @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 роки тому +1

      beneficial hai bro, jaroori nahi

  • @trading.helper-1997
    @trading.helper-1997 3 роки тому +3

    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 роки тому +2

      bhai 4 hi to hai "cccc"