Longest Palindromic Sub-string (LeetCode 5) | Full solution with examples | Study Algorithms

Sdílet
Vložit
  • čas přidán 26. 07. 2024
  • Longest Palindromic Substring is a programming challenge on LeetCode. A palindrome is a type of string that reads the same when reading left to right and right to left. An example of a palindrome can be “level”, “racecar”, “kayak” etc. Given a string, find the longest palindromic sub-string. You need to return just one string, and the characters should be contiguous. Watch the video to understand the problem in a simplified manner. I then work along with you to solve it first using a Brute Force approach, and then an efficient approach. All along with visuals and explanations.
    00:00 - Intro
    01:40 - Problem Statement and Test Case
    02:58 - Brute Force Solution
    03:45 - Efficient Solution
    05:41 - Dry-run of code
    📚 Links I talk about in the video:
    Actual problem on LeetCode: leetcode.com/problems/longest...
    Code on Github: github.com/nikoo28/java-solut...
    Test cases on GitHub: github.com/nikoo28/java-solut...
    📘 A text based explanation is available at: studyalgorithms.com/string/lo...
    To see more videos like this, you can show your support on www.buymeacoffee.com/studyalg...
    💻 Get Social 💻
    Follow on Facebook at: / studyalgos
    Follow on Twitter at: / studyalgorithms
    Follow on Tumblr at: / studyalgos
    Subscribe to RSS feeds: studyalgorithms.com/feed/
    #leetcode #programming #tutorial

Komentáře • 43

  • @plutomessi21
    @plutomessi21 Před 8 měsíci +14

    nikhil bhaiya, please never get demotivated by any negative comment's. I respect every second you have spent on your precious video's for the benefit of community. Thank you so much

    • @nikoo28
      @nikoo28  Před 8 měsíci +2

      thanks for your kind words...trying my best each day!! :)

  • @parthnoida
    @parthnoida Před 4 lety +13

    Finally someone is focusing on the algorithm rather than implementation

  • @kah2551
    @kah2551 Před měsícem +2

    I usually don't comment on videos, but the way you explain concepts and approach the problem is phenomenal, keep it up brother 💯
    Ignore the negative comments 🙃

  • @abhishekkumar4814
    @abhishekkumar4814 Před 4 lety +3

    Great tutorial sir!

  • @arjunjadhav6275
    @arjunjadhav6275 Před rokem +1

    great and understandable Solution

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

    Thank you very much for this amazing explanation

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

    Really helpful!!!

  • @boubacarboulkassoum2804

    Thanks to you !!

  • @jawahar-us6wl
    @jawahar-us6wl Před 2 měsíci

    Very neat explanation sir , Thank You

  • @user-qy2fm3pu2b
    @user-qy2fm3pu2b Před 20 dny

    Amazing man🔥

  • @harikalakshmisainannapanen181

    Sir you are the best 🙏🙏🙏🙏🙏

  • @az-op8oc
    @az-op8oc Před rokem

    Your voice is so cool, listenable 🥰

  • @mohdahasansiddiqui9063
    @mohdahasansiddiqui9063 Před 9 měsíci +1

    will it work for "ab" and "bb"?
    I think we should add this extra line as well.
    if (str.length() == 2) {
    return str.charAt(0) == str.charAt(1) ? str : str.charAt(0) + "";
    }

  • @subee128
    @subee128 Před 7 měsíci +1

    Thanks

  • @CanvasAndCompass
    @CanvasAndCompass Před 3 lety +1

    expaination is ...Lit !!!!!!!!!!!!!!!

  • @technicalstuff9595
    @technicalstuff9595 Před 6 měsíci +3

    string palindrome should be :
    string palindrome=str.substr(low+1,high-low-1);

  • @user-fj2cz9mg6n
    @user-fj2cz9mg6n Před 4 měsíci

    In a case where no palindrome substring is found then it will return the second char as string, But the function should return first char as string.
    Ex . str ="rfvym "
    Expected Output : "r"
    Original Output:"f"
    so for this we need add one condition at the end
    if(LPS.length()==1)
    return str.substring(0,1)
    else
    return LPS

  • @Sri_9991
    @Sri_9991 Před 4 měsíci

    the best

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

    How could i optimise its time complexity?

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

    The Time Complexity of this approach will be O(str.length()^2) I guess.

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

    Great tutorial bud , but could you please explain why we start with low = -1 for even length?

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

      we cannot have single midpoint for odd length string

  • @user-ph5ek8tg5l
    @user-ph5ek8tg5l Před měsícem

    Which headset are you using?

  • @SuriyaT3001
    @SuriyaT3001 Před rokem

    any one pls clarify me why HIGH is used in substring(),

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

      String.substring() is a java function which takes two arguments, starting index(inclusive) and ending index (not inclusive) and returns the subtring from the given string. In C++ its starting postion and the size of required substring.

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

      This is because substring return the value from start index to lastindex -1 postion. If we give HIGH -1. it will miss one character@@abhisheknandann

  • @gokulakannan3664
    @gokulakannan3664 Před rokem +2

    String palindrome = str.subString(low+1, high);
    Anyone plz explain.... why he put 'low+1' instead of 'low'
    and 'high' is excluding one.... so, he have put 'high+1' but... he put 'high' ?

    • @tushar9535
      @tushar9535 Před rokem +3

      in subString method, begin index is inclusive and end index is exclusive

    • @shanm-l3m
      @shanm-l3m Před měsícem

      while loop terminates when "low" variable and "high" index characters don't match. According to the problem we need palindromic substring so we make "low" increament by one this ensures that the sub string is palindromic.

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

    What is time complexity here?

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

    "ab" ka output kya aayega?

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

      Either a or b

    • @user-or6oh9is7y
      @user-or6oh9is7y Před 3 měsíci

      @@nikoo28 not working for ab and aa we need to handle that test case

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

    why start for loop with 1

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

      The logic of the solution is that you start at any character and then go both left and right directions. If you start at 0th element, then you cannot go left, only go right. So you will not be able to check palindromes.

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

      @@nikoo28 thanks for wonderful explanation

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

    here is the code
    class Solution {
    public String longestPalindrome(String s) {
    if (s.length() = 0 && high < s.length() && s.charAt(low) == s.charAt(high)) {
    low--;
    high++;
    }
    // low and high have gone one step too far, adjust for the correct substring
    String palindrome = s.substring(low + 1, high);
    if (palindrome.length() > lps.length()) {
    lps = palindrome;
    }
    // Even length palindromes
    low = i - 1;
    high = i;
    while (low >= 0 && high < s.length() && s.charAt(low) == s.charAt(high)) {
    low--;
    high++;
    }
    // low and high have gone one step too far, adjust for the correct substring
    palindrome = s.substring(low + 1, high);
    if (palindrome.length() > lps.length()) {
    lps = palindrome;
    }
    }
    return lps;
    }
    }

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

    Time complexity ki dhajiya udh gayi 😂

  • @Ajeetkumar-uo4hf
    @Ajeetkumar-uo4hf Před 11 měsíci

    Bhai solution khol ke kya bata rha hai code ni karne ata kya😂

    • @nikoo28
      @nikoo28  Před 10 měsíci +12

      if you understand the logic, writing code in any language isn't a problem. I go over the entire concept on how to approach the problem