DP 28. Longest Palindromic Subsequence

Sdílet
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

Komentáře • 396

  • @johncenakiwi
    @johncenakiwi Před 2 lety +91

    As soon as you explained the reversing part, it clicked. Amazing video as always. Thank you very much!!!😀

  • @PiyushSharma-ud9qk
    @PiyushSharma-ud9qk Před 2 lety +135

    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❤️❤️

    • @sdexstarlord
      @sdexstarlord Před 2 lety

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

    • @PiyushSharma-ud9qk
      @PiyushSharma-ud9qk Před 2 lety +6

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

    • @sankaranarayanankm7049
      @sankaranarayanankm7049 Před rokem

      will you please send the link of lcs lectuare

    • @sandeepmourya8922
      @sandeepmourya8922 Před rokem +6

      @@sdexstarlord are vedya, every every substring is also subsequence, but the opposite is not always true.

    • @pulkitjain5159
      @pulkitjain5159 Před rokem

      @@sdexstarlord bhai kyuki apan ko subsequence chaiye na , isliye , agar largest commom substring bolta tab second wali approach se kar dete

  • @Harshit126
    @Harshit126 Před 2 lety +14

    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!

  • @JeevanGaikwad-G1
    @JeevanGaikwad-G1 Před 2 lety +3

    Amazing. Thanks a lot for bringing up this DP series and putting hard work for a good explanation. Now DP problem look easy 😀

  • @anubhav_gupta_
    @anubhav_gupta_ Před 10 měsíci +7

    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

  • @mayank_singh_43
    @mayank_singh_43 Před rokem +7

    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

  • @sachinkore974
    @sachinkore974 Před rokem +1

    the moment u said char at start and char at end got the idea and able to solve the que. Thanks

  • @dorisalarushika5460
    @dorisalarushika5460 Před 2 lety +2

    I did it without watching explanation itself by the knowledge of previous videos truly thank u

  • @hetavvakani7025
    @hetavvakani7025 Před 2 lety +7

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

  • @jaaswindkotian6615
    @jaaswindkotian6615 Před rokem +1

    This was just simply amazing !!

  • @ranasauravsingh
    @ranasauravsingh Před 2 lety +1

    UNDERSTOOD...!!!
    Thank you striver for the video... :)

  • @kathanvakharia
    @kathanvakharia Před rokem +7

    Understood....Completed (28/56). HALF WAY THROUGH🙌🙌

  • @narendrakumariitb
    @narendrakumariitb Před 2 lety

    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

  • @cinime
    @cinime Před 2 lety +2

    Understood! What a fantastic explanation as always!!

  • @dipaligangawane980
    @dipaligangawane980 Před 2 lety +1

    You are amazing. Really wonderful Explanation.

  • @undergrad4980
    @undergrad4980 Před rokem

    Absolutely brilliant.

  • @shreyasnagabhushan4918

    understood bro, thanks a lot for making this question look so simple.

  • @radharamamohanakunnattur3035

    Understood, thank you for the awesome explanation

  • @raj_kundalia
    @raj_kundalia Před rokem

    Understood. Thanks for the video!

  • @ratinderpalsingh5909
    @ratinderpalsingh5909 Před 2 lety +1

    Understood, sir. Thank you very much.

  • @warriorgaming9509
    @warriorgaming9509 Před rokem

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

  • @gsampath8017
    @gsampath8017 Před 2 lety +3

    Thanks striver❣️

  • @gaura-krishna
    @gaura-krishna Před rokem

    Thank you very much brother. Thanks a lot. Accha lagta hai jab sawal ho jata hai...

  • @andreasleonidou3620
    @andreasleonidou3620 Před rokem

    Understood! Thanks!

  • @ritikshandilya7075
    @ritikshandilya7075 Před měsícem

    Amazing explanation , thanks Striver

  • @AlokLaha-te2pd
    @AlokLaha-te2pd Před 17 dny

    Thanks striver for explaining the reverse logic.

  • @prajwal8458
    @prajwal8458 Před 2 lety +12

    Correct Problem link: www.codingninjas.com/codestudio/problems/longest-palindromic-subsequence_842787

  • @chitreshmourya-sz6kb
    @chitreshmourya-sz6kb Před 5 měsíci

    gazab explanation bhai , understood just in a half video

  • @030-sham
    @030-sham Před rokem

    thank you striver

  • @VishalYadav-gk1kg
    @VishalYadav-gk1kg Před 11 dny +1

    Very nice explanation sir, Thank you!

  • @nguyengiahuy6292
    @nguyengiahuy6292 Před rokem +1

    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

  • @rishabhagarwal8049
    @rishabhagarwal8049 Před rokem

    Understand Sir, Thank you very much

  • @varunaggarwal7126
    @varunaggarwal7126 Před rokem

    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 .

  • @koocheukkeijacky9704
    @koocheukkeijacky9704 Před 9 měsíci

    after watching your previous video, I can solve this question on the fly before watching this videooo.!

  • @stith_pragya
    @stith_pragya Před 6 měsíci +1

    #UNDERSTOOD........Thank You So Much for this wonderful video.......🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @lakshaysharma6550
    @lakshaysharma6550 Před 2 lety +1

    UNDERSTOOD!!!🔥🔥🔥🔥🔥🔥

  • @himanshuagrawal8012
    @himanshuagrawal8012 Před 2 lety +5

    #UNDERSTOOD LCS is amazing..we can solve a lot of using that logic only...Thanks

  • @ssc_-kn6os
    @ssc_-kn6os Před 2 lety +2

    Problem Link : www.codingninjas.com/codestudio/problems/longest-palindromic-subsequence_842787?leftPanelTab=0

  • @AbhishekGupta-xz1gd
    @AbhishekGupta-xz1gd Před 9 měsíci

    Understood, great explanation

  • @akashkumarsingh3595
    @akashkumarsingh3595 Před 2 lety +2

    Understood🔥🔥

  • @DevashishJose
    @DevashishJose Před 6 měsíci

    Understood thank you so much

  • @vijayvardhan2698
    @vijayvardhan2698 Před rokem

    I came up with the solution without watching the video. Damn man, thanks !!!!!!

  • @shubhankar_naik
    @shubhankar_naik Před rokem

    Mind blowing solution

  • @VishalPatel_imvishal
    @VishalPatel_imvishal Před rokem +2

    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 :) ❤

  • @saimhatre89
    @saimhatre89 Před 2 lety +1

    Superb Sir, Understood

  • @enigma2777
    @enigma2777 Před rokem

    Amazing. understood

  • @amishasahu1586
    @amishasahu1586 Před 2 lety +7

    Hey Sir!!
    How do it print the longest palindromic substring.

  • @arnabroy2995
    @arnabroy2995 Před rokem +1

    This is so good ❤ now dp is easy because of you.. US ❤

  • @johncenakiwi
    @johncenakiwi Před 2 lety +5

    Hey Striver! Can you please do a video on LC 139 . Word Break??

  • @siddhantvishnu4309
    @siddhantvishnu4309 Před 5 měsíci

    Amazing!!!

  • @kartikrameshchavan6662
    @kartikrameshchavan6662 Před rokem +1

    Understood 🙏

  • @ayushgautam169
    @ayushgautam169 Před rokem

    understood striver !!

  • @shivangsaini3940
    @shivangsaini3940 Před rokem +1

    understood bhaiya ❤

  • @soniamalik4929
    @soniamalik4929 Před 2 lety +2

    Understood Sir

  • @deepakjain4481
    @deepakjain4481 Před 2 měsíci +1

    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

  • @abdallaalhag4425
    @abdallaalhag4425 Před 7 měsíci

    understood Thanks raj😁

  • @pavan_talluri6540
    @pavan_talluri6540 Před 2 měsíci

    Thanks!

  • @parshchoradia9909
    @parshchoradia9909 Před rokem

    Understood Sir!

  • @vaishnavithakur6460
    @vaishnavithakur6460 Před 2 lety +1

    Understood!!!

  • @Hello-ro6hr
    @Hello-ro6hr Před 2 lety

    UNDERSTOOD ...

  • @theresilientpianist7114
    @theresilientpianist7114 Před 11 dny +1

    gazabbbbbb yrrrr❤❤🔥🔥🙌🙌🙏🙏✨✨

  • @mantrarajgotecha3055
    @mantrarajgotecha3055 Před 2 lety +1

    Understood !!

  • @harshitjaiswal9439
    @harshitjaiswal9439 Před rokem

    Understood!

  • @chanchalroy3417
    @chanchalroy3417 Před 6 měsíci

    Halfway done 👍 🙌

  • @shubhigupta5689
    @shubhigupta5689 Před rokem

    Understood🌻

  • @adarshkumar167
    @adarshkumar167 Před 2 lety +1

    Just after writing the second string, my mind clicked.

  • @himanshuagrawal8012
    @himanshuagrawal8012 Před 2 lety +5

    Please update the link with LPS , its actually of LCS

  • @vipulaggarwal7489
    @vipulaggarwal7489 Před 2 lety +1

    understood!

  • @VikashYadav-px8ei
    @VikashYadav-px8ei Před rokem +1

    Understood 🎉

  • @saurabhrthakur
    @saurabhrthakur Před 3 měsíci

    UNDERSTOOD!

  • @sujalgupta6100
    @sujalgupta6100 Před rokem

    Understood.

  • @adebisisheriff159
    @adebisisheriff159 Před 6 měsíci

    Understood bro!!

  • @pulkitchausali1354
    @pulkitchausali1354 Před rokem

    "Solved without seeing the video " logic is improving day by day

  • @sanamdeepsingh7914
    @sanamdeepsingh7914 Před 2 lety

    understood bhaiya

  • @varnitgupta706
    @varnitgupta706 Před 11 měsíci

    UNDERSTOOD

  • @ishanminhas4676
    @ishanminhas4676 Před 2 lety

    so good

  • @yeswanthh5068
    @yeswanthh5068 Před rokem +1

    Understood 🙂🙂💚

  • @shauryamalik7519
    @shauryamalik7519 Před 11 měsíci

    understood very well, but what to do if I have to return the Longest Palindromic Substring and not the length

  • @Whyush7
    @Whyush7 Před 2 lety +2

    please update the problem link in the description :)

  • @pratyakshsaxena7420
    @pratyakshsaxena7420 Před 11 měsíci

    Understood ❤

  • @chitranshjain5075
    @chitranshjain5075 Před 2 lety

    Understood. 🤟🏻

  • @prabhakaran5542
    @prabhakaran5542 Před 3 měsíci +1

    Understood❤

  • @arihantjammar8888
    @arihantjammar8888 Před 9 měsíci

    Understood 😊

  • @udatyadeb101
    @udatyadeb101 Před rokem +1

    understood, did this by myself.

  • @aditithakur6226
    @aditithakur6226 Před rokem

    Understood Sir.

  • @satyampande684
    @satyampande684 Před 2 lety

    understood!!

  • @fettuccine794
    @fettuccine794 Před 6 měsíci

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

  • @mahir_labib
    @mahir_labib Před 7 měsíci

    mind blown

  • @_hulk748
    @_hulk748 Před rokem

    understood sir🙏🙏❤️❤️

  • @bhushanmilindborole9616

    Mindblown bhai 🔥

  • @amansamal8233
    @amansamal8233 Před 2 lety +2

    ❤❤ understood

  • @Ajay-ei2jo
    @Ajay-ei2jo Před rokem

    Understood👍

  • @rahulrrahulr2151
    @rahulrrahulr2151 Před rokem +1

    Where I can get the java code for this problem sir I cannot find in the description notes

  • @piyush2750
    @piyush2750 Před rokem

    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

  • @bhuoneparmar5098
    @bhuoneparmar5098 Před 10 měsíci

    this is the best dp series man Understood bhaiya ❤❤

  • @priyanshvatsal9791
    @priyanshvatsal9791 Před rokem

    Understood 🥰

  • @75dhruvkhanna71
    @75dhruvkhanna71 Před rokem +1

    the reversing method won't work for palindromic substring

  • @Nitro-kx7ok
    @Nitro-kx7ok Před rokem

    Understood sir🙏❤🙇‍♂

  • @adityasaxena6971
    @adityasaxena6971 Před rokem

    Understood Sirr

  • @UECAbhishekKumar
    @UECAbhishekKumar Před rokem

    Understood