Longest palindromic substring | Dynamic programming

This video explains how to find longest palindromic substring from a given string. This is a very frequently asked programming interview and coding round question. I have explained two techniques here. One is bruteforce by generating all possible substrings and checking if it is palindrome or not and storing the longest substring which is a palindrome found. My audio quality is bad due to damaged-mic so kindly bear for this video. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
CODE LINK: www.geeksforgeeks.org/longest...

Пікірлер: 479

  • @techdose4u
    @techdose4u9 ай бұрын

    🟣 JOIN our 𝐋𝐈𝐕𝐄 𝐢𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐭𝐫𝐚𝐢𝐧𝐢𝐧𝐠 𝐩𝐫𝐨𝐠𝐫𝐚𝐦 through whatsapp query: +91 8918633037 🔴 𝐂𝐡𝐞𝐜𝐤𝐨𝐮𝐭 𝐚𝐥𝐥 𝐨𝐮𝐫 𝐂𝐨𝐮𝐫𝐬𝐞𝐬: techdose.co.in/

  • @hey.............
    @hey.............4 жыл бұрын

    Kudos for going through the whole input string instead of saying so on and so forth👍

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    😂

  • @lionelmesssi2959

    @lionelmesssi2959

    3 жыл бұрын

    Bro tomorrow my exam is there , exam will consist of coding question in Java. Dynamic programming also there can you help me for exam .

  • @tekbssync5727

    @tekbssync5727

    3 жыл бұрын

    @@lionelmesssi2959 I don't think there will be any football match tomorrow .

  • @ganesh3761

    @ganesh3761

    2 жыл бұрын

    @@tekbssync5727 😅😁

  • @ragas_
    @ragas_4 жыл бұрын

    Fact : This guy alone is responsible for more placement offers than all college professors combined ! Change my mind

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks

  • @ankitparashar8730

    @ankitparashar8730

    2 жыл бұрын

    Professor kaha padhata Sab chutiyapa hai professor

  • @prajwalraut7480

    @prajwalraut7480

    Жыл бұрын

    woahhhh today also this one is best explaination!!!!!!!!!!!!!!

  • @KevinLin45

    @KevinLin45

    Жыл бұрын

    🧠 yelo change krlo :)

  • @KushalBhatia
    @KushalBhatia3 жыл бұрын

    Please give this man a medal. Thank you for going through the whole string dry run

  • @thecritiquer5976
    @thecritiquer597611 ай бұрын

    I knew from reading other explanations that it was the explanation itself that was hindering everyone including myself. You are so far the only person that actually explained how the dynamic approach works clearly.

  • @jrajesh11
    @jrajesh112 жыл бұрын

    What a great presentation and explanation ! You are just going frame by frame and bringing clarity all the way down deep! Keep doing such great videos for demystifying complex algos.

  • @sam_s3344
    @sam_s33443 жыл бұрын

    Thanks a million for making such an incredible explanation for a GFG code. Sometimes they have good codes, but no explanations or the explanations are not clear enough. This is just so well explained. All your videos are helping so many of us. Keep up the great work!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @sathvikrijo
    @sathvikrijo4 жыл бұрын

    One of the best explanations for this question, for dp beginners🎉🍾 thanks a lot man💐👍

  • @yuganderkrishansingh3733
    @yuganderkrishansingh37334 жыл бұрын

    Bro this is the best explanation for the question and I finally understood it. Explaining is an art and making others understand is a superpower and you clearly has it. The video was crisp and so easy to follow. Just loved it. Pls keep making more videos. I know you might be busy with other stuff(I am sure u might be working for some top notch company and if not then you are surely going to be very soon) but pls keep making more videos.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Yea sure :)

  • @aslanfeng413
    @aslanfeng4134 жыл бұрын

    THE best explanation I’ve heard so far!!!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    :)

  • @lionelmesssi2959

    @lionelmesssi2959

    3 жыл бұрын

    Bro tomorrow my exam is there , exam will consist of coding question in Java. Dynamic programming also there can you help me for exam .

  • @bookalicious9849
    @bookalicious98493 жыл бұрын

    Excellent explanation ! As a newbie in dp i really was struggling to understand this concept but you made my life easier !!!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @utkarshsrivastava7885
    @utkarshsrivastava78853 жыл бұрын

    You are just explaining the whole concept with patience . thanku for clearing me this algo ,you are just my fav man❤️

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @paulcurran3661
    @paulcurran36613 жыл бұрын

    Thank you for this, I've had trouble understanding this algorithm in other videos but this is the best explanation I've seen.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @JSInstructor
    @JSInstructor3 жыл бұрын

    Thank you so much for the video! I hadn't truly understood the dynamic programming approach to the max palindrome substring problem until I watched your explanation. Thanks again!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @meetmodi2487
    @meetmodi24873 жыл бұрын

    New to DP. Was trying to get this approach for 2 hrs.Thanks alot for the explanation.Explained so well,no need to upload the code!

  • @ayyappareddy4461
    @ayyappareddy44612 жыл бұрын

    I watched lot of videos for this problem but no one explained like the way you explained.thank you sir

  • @9669sumit
    @9669sumit2 жыл бұрын

    Now i know why your video is on the top when searched for this problem. Thanks for such good content.

  • @abdallaalhag4425
    @abdallaalhag44255 ай бұрын

    I really love how you draw and go through the DP table. I don't see many people explain it that way and it can be very hard to visual or trace through at first. Thanks So much!

  • @chillegaming8250
    @chillegaming82503 жыл бұрын

    This is gold! Thank you for that clear explanation.. your DP series is the best out there!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome ❤️

  • @manojrajasekar6035
    @manojrajasekar60354 жыл бұрын

    Best Explanation ever ! Please continue doing this. Thank you :)

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Yea..... Thanks :)

  • @ayeshaadhikari6123
    @ayeshaadhikari61232 жыл бұрын

    No amount of thanks is ever going to repay the help you're lending us :) Thanks a tonne sir! Keep growing

  • @TravelWithAmitDubey
    @TravelWithAmitDubey4 жыл бұрын

    I liked the way you explained this problem, best explanation I ever found for this problem.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @abhishekbaral4699
    @abhishekbaral46993 жыл бұрын

    really understood the concepts of DP with clarity. Never visualized like this before.. Thanks

  • @tigerbear3038
    @tigerbear30382 жыл бұрын

    Excellent explanation! Really appreciate that you went step by step even though it seemed tedious but it made it easy to follow.

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks ☺️

  • @ZiadGholmish
    @ZiadGholmish4 жыл бұрын

    Honestly, the best one explained this question

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @ajeetshankar7946
    @ajeetshankar79462 жыл бұрын

    Quality content with best explanation compared to every other video, special thanks to you!!

  • @satvik_b
    @satvik_b Жыл бұрын

    good explanation! The reason this is a dp problem is that it has over lapping sub problems. For substring from index 1,5 we need to check if substr from index 2,4 is palindrome or not and for substr 0,6 we would not compute for substr 1,5. So it will take O(1) time instead of O(n).

  • @omerfarukbuyukbas
    @omerfarukbuyukbas2 жыл бұрын

    It is really the best explanation I've ever seen in this subject! Thank you!

  • @atefnazi753
    @atefnazi7533 жыл бұрын

    How you explain these things so clearly..... I must say you are very underrated

  • @shrutibansal5261
    @shrutibansal52612 жыл бұрын

    best video I have seen so far for understanding dp using a table

  • @jaatharsh
    @jaatharsh3 жыл бұрын

    Thanks, TechDose, crisp n clear explanation, exactly what I was looking for, keep uploading!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @harshitbhatt5875
    @harshitbhatt58753 жыл бұрын

    Please, if possible, explain how you approach a problem and come up with solutions too as it'll help in developing our programming logic as well. Thanks for the great vids, keep em comin'! :)

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Sure :)

  • @beautifultime9031
    @beautifultime90314 жыл бұрын

    The only video that explains the DP matrix and each step of the algorithm. Thanks so much.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :) I always explain the intuition and steps of dp matrix with reason for formulations.

  • @arjunreddy3615
    @arjunreddy36154 жыл бұрын

    Explained very well.. I have written the code using the instructions and it is working well strings of random sizes.. Thank you...

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @mychannelyashi2441
    @mychannelyashi24413 жыл бұрын

    This channel is now becoming my fav channel,,, thanks for explaining the concept so easily .

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @lionelmesssi2959

    @lionelmesssi2959

    3 жыл бұрын

    Bro tomorrow my exam is there , exam will consist of coding question in Java. Dynamic programming also there can you help me for exam .

  • @krithickkrishnagiri6371
    @krithickkrishnagiri63714 жыл бұрын

    finally got the concept behind the dynamic programming. Thank you

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @lionelmesssi2959

    @lionelmesssi2959

    3 жыл бұрын

    Bro tomorrow my exam is there , exam will consist of coding question in Java. Dynamic programming also there can you help me for exam .

  • @HarinathSrinivas
    @HarinathSrinivas4 жыл бұрын

    For length 3 also it is enough to check only if characters are equal, like length 2 as diagonal is always 1.

  • @radhu8
    @radhu83 жыл бұрын

    Can't explain how thankful I am for your explanations!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @lionelmesssi2959

    @lionelmesssi2959

    3 жыл бұрын

    Bro tomorrow my exam is there , exam will consist of coding question in Java. Dynamic programming also there can you help me for exam .

  • @AbhishekA-81
    @AbhishekA-813 жыл бұрын

    One of the best teachers found on KZread 👍🏻

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @shubhammahindru3563
    @shubhammahindru35639 ай бұрын

    This is the best expalantion for this question on youtube, thanks mate

  • @jhilikkundu3158
    @jhilikkundu31583 жыл бұрын

    Noone says that row is the starting pos and column ending. You just clear my doubt in first few minutes. Thank you again.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @prasitamukherjee5864
    @prasitamukherjee58644 жыл бұрын

    Thank you so much! It was crystal clear.

  • @aakashparmar560
    @aakashparmar5602 жыл бұрын

    God-level explanation.!! If possible please keep these videos coming.

  • @kumarc4853
    @kumarc48532 жыл бұрын

    thank you Tech dose, I cracked google and linkedin. Learned a lot from your videos and explanations

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Congratulations 🤗💥🎉

  • @0laoye
    @0laoye7 ай бұрын

    what a great explanation! love your divide and conquer techniques

  • @darshansimha2166
    @darshansimha21663 жыл бұрын

    The best explanation so far!

  • @AmazingWorld-fw9oc
    @AmazingWorld-fw9oc3 жыл бұрын

    I'm glad I found this channel.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    😊

  • @Star_Bawa9
    @Star_Bawa92 жыл бұрын

    Thanks , you get straight to the point continue like this

  • @prateeksrivastava1577
    @prateeksrivastava15773 жыл бұрын

    One of the best explanation for this problem.....Thanks bro...!!!!

  • @PujaKumari-rp5sg
    @PujaKumari-rp5sg2 ай бұрын

    If I had a teacher like you during college, I would never doubt myself that I can't learn DSA.

  • @CodeSuccessChronicle
    @CodeSuccessChronicle3 жыл бұрын

    thanks for going through the whole string. Thank you for all your efforts 🙏

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @lionelmesssi2959

    @lionelmesssi2959

    3 жыл бұрын

    Bro tomorrow my exam is there , exam will consist of coding question in Java. Dynamic programming also there can you help me for exam .

  • @shubhamsinha5664
    @shubhamsinha56644 жыл бұрын

    if there are more than one substring of the same length then how to return the substring which occurs first ( with the least starting index ) ?

  • @dattatreyapujar4068
    @dattatreyapujar40684 жыл бұрын

    Nice work. Hope to see your channel grow beyond numbers.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks buddy :)

  • @rajeshseptember09
    @rajeshseptember092 жыл бұрын

    outstanding explanation man !!! Keep up the great job !

  • @Surya-np1bb
    @Surya-np1bb Жыл бұрын

    Awesome buddy! Thanks for this

  • @saicharanpinninti548
    @saicharanpinninti5484 ай бұрын

    very good explanation and this was my first dp problem really understands really well

  • @collins4359
    @collins43592 жыл бұрын

    thanks for the explanation. was so good I didn't need help writing the code.

  • @yousufahmed985
    @yousufahmed9856 ай бұрын

    You must be really smart to come up with solutions like these

  • @sakshiramsinghani5284
    @sakshiramsinghani52843 жыл бұрын

    Thanks a lot, you've explained it really well. It's very helpful.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @sanskaripatrick7191
    @sanskaripatrick71912 ай бұрын

    best explanation of this dp problem. Thank you so much🙏

  • @tanmaysatsangi131
    @tanmaysatsangi1313 жыл бұрын

    Hello Tech Dose, can u please explain why this formula can't be apply for string length less than 3.

  • @gmdz0ji336
    @gmdz0ji3364 жыл бұрын

    Best explanation ever!! Thank you sooo much :D

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @crankyinmv
    @crankyinmv3 жыл бұрын

    Thanks. Very useful. However, brute force can be done in O(n^2): for(0 through length of string-1) find the longest palindrome whose middle is in that position if(longer than the previous max) save the start and end indexes return substring at the saved start and end indexes

  • @geeteshchallur1034

    @geeteshchallur1034

    2 жыл бұрын

    So how is dp appoach better than this approach?

  • @manishbolbanda9872
    @manishbolbanda9872 Жыл бұрын

    wow this is gold. such a great solution. Thanks a ton. i usually approach dp problem with rec first then memoization then tabulaiton. bt this is more like concrete tabulation solutiion.

  • @guruvigneshnagaraj8501
    @guruvigneshnagaraj85014 жыл бұрын

    Perfect Explanation. Thank you!!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @bonagiripraneeth9502
    @bonagiripraneeth95023 жыл бұрын

    Sir I didn't understand how to loop through the array, first we checked for (0,2) then for (1,3) and then for (2,4)... How to increment both I and j and after reaching end for j , we again set set I to some other index. How and which loops should be used?

  • @satweek123
    @satweek1234 жыл бұрын

    Really good explanation @techdose. i feel more confident with DP now

  • @anitapanda9267
    @anitapanda92673 жыл бұрын

    Sir...how beautifully u explained the logic...Thank you

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @piyusgupta9498
    @piyusgupta94984 жыл бұрын

    Awesome explanation, finally I understood, thanks.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @msreenivasaprasad2214
    @msreenivasaprasad22144 жыл бұрын

    Very clearly explained . Thanks for helping me out brother.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome

  • @ashwinraj8918
    @ashwinraj89184 жыл бұрын

    Flawless explanation!!. Thank You!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @sangramkesharighosh1698
    @sangramkesharighosh1698 Жыл бұрын

    Your explaining skill is mind-blowing 😁

  • @pathevolutionnetwork
    @pathevolutionnetwork3 жыл бұрын

    This was great !! Thanks for helping me understand this one

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @diasposangare1154
    @diasposangare11543 ай бұрын

    the best explanation i found

  • @bhumberg3243
    @bhumberg32433 жыл бұрын

    The BEST explanation ...tqsm

  • @Star_Bawa9
    @Star_Bawa92 жыл бұрын

    That was a brilliant Explaination...

  • @vivek.tiwary
    @vivek.tiwary3 жыл бұрын

    Could not find better explanation than this.

  • @ahasunos5914
    @ahasunos59142 жыл бұрын

    He made this problem a cake walk. Thanks man.

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Welcome 😀

  • @ahasunos5914

    @ahasunos5914

    2 жыл бұрын

    @@techdose4u 🙌

  • @TUHINT
    @TUHINT3 жыл бұрын

    any idea about how t print the String, max length will generate in dp[0][s.length()-1]

  • @pavanrao5896
    @pavanrao58964 жыл бұрын

    One of the best explanations I've come across! Do you have a video for O(n) complexity as well?

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    No, till now I have not made any video on manacher's algo.

  • @deveshagarwal660
    @deveshagarwal6604 жыл бұрын

    Amazing video and nicely explained. Great job

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @rahul88632
    @rahul88632 Жыл бұрын

    Thanks for simple explanation

  • @ankitdoot6186
    @ankitdoot61864 жыл бұрын

    In fact, we could solve it in O(N2) time without any extra space. We observe that a palindrome mirrors around its center. Therefore, a palindrome can be expanded from its center, and there are only 2N-1 such centers. You might be asking why there are 2N-1 but not N centers? The reason is that the center of a palindrome can be in between two letters. Such palindromes have even number of letters (such as “abba”) and their center are between the two ‘b’s. Since expanding a palindrome around its center could take O(N) time, the overall complexity is O(N2). Source : InterviewBit

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Nice :)

  • @hkharryfunk
    @hkharryfunk3 жыл бұрын

    What tool are you using to draw on screen?

  • @anukriti5648
    @anukriti56482 жыл бұрын

    Well explained ✨😃Thankyou..

  • @mukeshbalaji04
    @mukeshbalaji043 жыл бұрын

    you have the best dynamic programming playlist ever

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @siddhantgoswami6651
    @siddhantgoswami66514 жыл бұрын

    will you please make a conceptual video on manacher's algorithm with pseudocode, i am waiting for the same, thanku

  • @vancedaccount7951
    @vancedaccount7951 Жыл бұрын

    Thx for this Video man.

  • @sureshgarine
    @sureshgarine2 жыл бұрын

    very nice explanation. thank you

  • @RishabKhuba
    @RishabKhuba2 жыл бұрын

    Can someone explain on what basis did we consider the longest palindrome from 2 to 7 after filling the table?

  • @AkshayAnil0-1
    @AkshayAnil0-12 жыл бұрын

    Is there any space optimized method too ?

  • @nikhilkaushal1615
    @nikhilkaushal16152 жыл бұрын

    Nicely explained! Thank you!

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Welcome 😀

  • @padmaraghunathan4849
    @padmaraghunathan48493 жыл бұрын

    Such a great teacher 🙏🙏🙏🙏🙏

  • @hassaankhalid873
    @hassaankhalid8732 жыл бұрын

    This was great, sir. Thank you.

  • @rohansamanta7819
    @rohansamanta78193 жыл бұрын

    Sir can you please explain how do I iterate the loop though ?

  • @NirmalSilwal
    @NirmalSilwal Жыл бұрын

    Java solution | DP thank you for the great explanation too :) 🔥 public String longestPalindrome(String s) { int n = s.length(); if (n == 1) return s; // dp[1][2] represents substring of s in range [1,2] inclusive is palindrome or not // 1 indicates palindrome in dp array and 0 represents not a palindrome int[][] dp = new int[n][n]; int start = 0, end = 0; // index of answer in s for (int i = 0; i // for upper triangular matrix for (int j = i; j >= 0; j--) { // end position in s if (i == j) dp[i][j] = 1; else if (i - j == 1) { // for substring of length 2 in upper traingular matrix if (s.charAt(i) == s.charAt(j)) dp[i][j] = 1; } // check border character and substring inside current border indicated by i and j else if (s.charAt(i) == s.charAt(j) && dp[i - 1][j + 1] == 1) { dp[i][j] = 1; } // tracking maximum length for substring with index in s if (dp[i][j] == 1 && i - j > end - start) { start = j; end = i; } } } return s.substring(start, end + 1); }

  • @harshpanwar1550
    @harshpanwar15503 жыл бұрын

    Thank u so much sir.....You explained in a wonderful way!!!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @hariikrishnan
    @hariikrishnan2 жыл бұрын

    This was asked today for our placement test. I was shocked. BTW it was for an SDE internship for 18k/month

  • @srinaath9845
    @srinaath98454 жыл бұрын

    Thank you bro. Awesome explanation 👏

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @kentsang9376
    @kentsang93763 жыл бұрын

    Very nice explanation, thank you so much!!!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)