Longest palindrome substring - LeetCode Interview Coding Challenge [Java Brains]

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ส.ค. 2024
  • Have you seen the new Java Brains? Check out www.javabrains.io now for awesome courses and content!
    Interview Question: Find the longest palindrome substring from the given string
    Leetcode link: leetcode.com/p...
    Solution Gist: gist.github.co...
    Difficulty level: Medium
    Language: Java
    Welcome to the Java Brains Java Interview challenge series of videos where we tackle various coding challenges ranging from the beginner level to advanced level - especially in the context of coding interviews.
    Any coding problems you would like me to explore? Please let me know in the comments what you think!
    #java #interview #javabrains

ความคิดเห็น • 166

  • @robdai
    @robdai 4 ปีที่แล้ว +104

    "resultLength = end - begin - 1;"
    it is not due to 0 indexing of Java. It is math. While finding inclusive length between two points you have to add 1 to its difference. eg. ZABBAV will be 4 - 1 + 1 = 4 chars long . You did "begin--" and "end++" before you break out of while loop which offsets the begin-end range by 2 units. Your correct start is "begin + 1" and correct end is "end - 1". The inclusive length of the substring thus is (end - 1) - (begin + 1) + 1 (due to inclusive range) which is equal to "end - begin - 1".
    Apart form that, a big fan of your teaching. Your tutorials helped me land my current job.

    • @RavindraKumar17
      @RavindraKumar17 4 ปีที่แล้ว

      Thanks... That was actually helpful. :)

    • @harinijeyaraman8789
      @harinijeyaraman8789 4 ปีที่แล้ว

      Found this really helpful !! Thanks :)

    • @shafiaaskari5949
      @shafiaaskari5949 3 ปีที่แล้ว

      This is really helpful

    • @ankitaburbure8142
      @ankitaburbure8142 2 ปีที่แล้ว

      Your comment actually helped to understand the logic behind the value of resultLength.. :)

    • @RibenaLion
      @RibenaLion 2 ปีที่แล้ว +2

      I rarely reply on TH-cam.. but thanks for this!

  • @crteezy
    @crteezy 4 ปีที่แล้ว +113

    I watched your previous palindrome video last month on the day before my interview and guess what? I got 4 hackerrank problems to solve and 2 of them were about palindromes !! AND I GOT THE JOB 😍
    Thank you sir!

    • @raniverla2731
      @raniverla2731 4 ปีที่แล้ว +1

      can we get that 4 four hackerrank problems?

    • @crteezy
      @crteezy 4 ปีที่แล้ว

      @@raniverla2731 I actually don't have access to them now. There were 2 different palindrome ones and some other 2 which I don't remember anymore

    • @rollinOnCode
      @rollinOnCode 2 ปีที่แล้ว +1

      good job!

  • @SpaceTimeBeing_
    @SpaceTimeBeing_ 4 ปีที่แล้ว +16

    Yeah! Leetcode problems from Java Brains! Can't wait for those incredible explanations haha

  • @NimbuCB
    @NimbuCB 3 ปีที่แล้ว +27

    Does anyone feel - What the hell I am doing with my life?Is our life all about all these LC codes.....Does this give you fulfillment? Philosophy apart .....Thankyou for the good explanation

    • @nameunknown007
      @nameunknown007 3 ปีที่แล้ว +2

      It is beautiful if you like solving things. It's practicing your analytical skills and identify patterns. Mathematicians and Physicists use these skills everyday to make things efficient and find solutions. However it's not fun if you don't like practicing/working on algorithms.

    • @MereAYT
      @MereAYT 3 ปีที่แล้ว

      The concepts underlying computer science are fascinating, and the applications of programming in solving real-world problems are exciting. Those things draw a lot of people into the field. Unfortunately, we have to pretend to be a parser to get a first round technical interview every time. The further along you are in your career, the more distanced you become from this, so it hits you like a bucket of cold water. You're not alone in the feeling. Fortunately, the interview usually becomes more substantial and conceptual after that, and most languages abstract this stuff away.

    • @GenjaOrigins
      @GenjaOrigins 2 ปีที่แล้ว +4

      I only care about the interview i will not spend not even 1 sec after i get the job

    • @ramfattah275
      @ramfattah275 2 ปีที่แล้ว

      I feel you man, its all part of the game.

    • @bobweiram6321
      @bobweiram6321 10 หลายเดือนก่อน

      It's the stupidest game.

  • @Daniyay
    @Daniyay 3 ปีที่แล้ว +42

    i understand your solution, but i feel like i would have never been able to think of this solution on my own. its discouraging sometimes :( thanks for explaining everything so clearly!

    • @mkbhd2
      @mkbhd2 3 ปีที่แล้ว +8

      Many are with you, so don't worry. Our practice should make us better hopefully. Good luck!

    • @Satenc0
      @Satenc0 2 ปีที่แล้ว +1

      Bro just learn Manachers algorithm and you will impress if this appears on an interview xD

    • @ngonimugandani4504
      @ngonimugandani4504 ปีที่แล้ว

      i always write my own solutions that i understand better, challenge is they might not be efficient

  • @Granta_Omega
    @Granta_Omega ปีที่แล้ว +1

    I did a very complex solution where there was a nested loop and I put how many times I had a roll it back and it passed every test case. However, it would not be accepted by LeetCode because it runs out of time because it is way too complex. I'm glad I found this video to give me a better way to try it.

  • @prafullakh
    @prafullakh 3 ปีที่แล้ว +5

    Your video really helped. Thank you very much. Just a suggestion: if you can take an example and walk through the solution towards the end, that would be awesome

  • @vinayakgupta1
    @vinayakgupta1 3 ปีที่แล้ว +3

    Man this is the best explanation for this question, I have been struggling with this from so long. Thanks a lot!!

  • @annonymousarmadillo1094
    @annonymousarmadillo1094 2 ปีที่แล้ว +2

    Please correct me if I'm wrong but there's no need to call expandRange twice. You can simply check if, and only if, either the character left or right of the center is equal to the center. Because you can only have a palindrome with a 2-character center if both characters at the center are equal, otherwise it's no longer a palindrome.

    • @aakashgupta7211
      @aakashgupta7211 ปีที่แล้ว +1

      Yes, I used:
      expand(s, i, i);
      if(s.charAt(i) == s.charAt(i+1))
      expand(s, i, i+1);
      And it worked fine for all the testcases.

  • @johnny5gr
    @johnny5gr 4 ปีที่แล้ว +2

    Very cool video, as always. One small mistake in your explanation in my opinion. The result length is equal to end - begin - 1 not because zero index but because when one subtracts end - begin, it gives you the difference and not the number of indexes included. For example if we have 1, 2, 3, 4, 5, 6 and begin = 2 and end = 5, the length is still equal to end - begin - 1.

  • @abhijitpatwardhan8406
    @abhijitpatwardhan8406 3 ปีที่แล้ว +2

    Thank you for the excellent explanation! Special thanks for the "clean code" and expressive method/variable names-- rare to see that in LeetCode problem solution videos!

  • @kuttikrishnankodoth1463
    @kuttikrishnankodoth1463 2 ปีที่แล้ว +1

    I see you are using apple pen for yor drawing purpose , how you record the screen when you writing ?? :)

  • @MegaValture
    @MegaValture 4 ปีที่แล้ว +2

    I really love these videos. Your explanations are extremely helpful and easy to understand, please make more!

  • @ayushisinghal2339
    @ayushisinghal2339 3 ปีที่แล้ว

    Very helpful. You have explained it in detail to make it easy for everyone to understand. Thank you!

  • @shafiaaskari5949
    @shafiaaskari5949 3 ปีที่แล้ว

    This explanation was so easy and so well explained. I am really happy I found this video. Thanks, Java Brains !!

  • @vartikasingh6812
    @vartikasingh6812 3 ปีที่แล้ว +1

    we are running expandRange twice for even and odd length. Can't we put a check if(length %2) and accordingly call one of them? If we can't then please provide an explanation. Can't find an answer to this

  • @TheSumitSagar
    @TheSumitSagar 4 ปีที่แล้ว

    please keep on these leetcode series they are really helpful and fun to watch.

  • @jordanhumphrey6379
    @jordanhumphrey6379 3 ปีที่แล้ว

    thanks so much for this explanation, it was very thorough and I was able to understand it. Take my upvote!

  • @youssefmyh
    @youssefmyh 3 ปีที่แล้ว

    That's the best explanation for the palindrome.
    You are perfect 😍😍😍😍😍

  • @ALEEMKHAWAR1
    @ALEEMKHAWAR1 2 ปีที่แล้ว

    Thanks kaushik for such a wonderful explanation , big fan of teaching style

  • @AmanRaj-tj7lj
    @AmanRaj-tj7lj 3 ปีที่แล้ว +1

    Great!!
    Feedback: Do a walkthrough of the code at then end.

  • @ankitaburbure8142
    @ankitaburbure8142 2 ปีที่แล้ว

    I am glad I found your videos...:) your explanation actually helps to understand efficient solution ..

  • @RaviAdhav
    @RaviAdhav 3 ปีที่แล้ว

    Kaushik, Since we want to know the longest palendrome. Why cant we do this?
    1) Try to find if the entire string (with length say L) is palendrome.
    2) If 1 is not true, check that if out of the two possible strings with length = L - 1, is any one a palendrome.
    3) If 2 is not true, check that if out of the three possible strings with length = L - 2, is any one a palendrome.
    4) If 3 is not true, check that if out of the four possible strings with length = L - 3, is any one a palendrome.
    and so on...

  • @knrakeshkumar
    @knrakeshkumar 4 ปีที่แล้ว +2

    You are amazing! Teacher needs to learn from you how to teach as well.

  • @bewareofnikhil
    @bewareofnikhil 2 ปีที่แล้ว

    There exists an algorithm to solve this in O(n) time - Manacher's agorithm. But nice explanation of the O(n^2) solution.

  • @sriraghavsira7149
    @sriraghavsira7149 4 ปีที่แล้ว +1

    Please do a video series on Big O Notation and Time Complexity.

  • @akashchoudhary5895
    @akashchoudhary5895 ปีที่แล้ว

    great explanation and very clean code

  • @chandrakanthdudela5515
    @chandrakanthdudela5515 4 ปีที่แล้ว

    Thanks a lot for the video! Only part which I couldn't understand is why we're doing "end - begin -1" in the if block.

    • @evolvingnoobninja2431
      @evolvingnoobninja2431 4 ปีที่แล้ว +1

      Assume the while loop breaks when begin=2 and end=5. That means the str.charAt(begin)==str.charAt(end) condition was satisfied for begin=3, end=4. The length of the palindromic string then is (4-3+1). If we write it in the current begin and end variable values it will be ( (4-3+1) = (4+1-1)-(2+1)+1 = (5-2-1)) or (end-begin-1)

  • @harinijeyaraman8789
    @harinijeyaraman8789 4 ปีที่แล้ว

    Amazing content! Found this explanation very helpful !

  • @divitvivit8803
    @divitvivit8803 4 ปีที่แล้ว

    Simple and easy to understand could be:
    public class LongestPalindromeInString {
    public static void main(String[] args) {
    String s = ("1712332181");
    String longestPalindrome = "";
    if (s == null || s.trim().isEmpty()) {
    longestPalindrome="";
    }
    for (int i = 0; i < s.length(); i++) {
    for (int j = i; j < s.length(); j++) {
    String str = s.substring(i, j + 1);
    if (new StringBuilder(str).reverse().toString().equals(str)
    && longestPalindrome.length() < str.length()) {
    longestPalindrome = str;
    }
    }
    }

    System.out.println("Longest Palindrome Sequence :" + longestPalindrome);
    }
    }

  • @alittlecoding
    @alittlecoding 2 ปีที่แล้ว

    awesome explanation. I just implemented your logic with both python and java to practice and understand more.

  • @Anubis10110
    @Anubis10110 4 ปีที่แล้ว

    You are the most amazing teacher !!
    If you have some time try to explain backtracking and DP in simple way as usual.
    Thank you so much

  • @dnavas7719
    @dnavas7719 4 ปีที่แล้ว +2

    Thanks for the video. Question. is the time complexity O(n^2) because you loop n times and check very character in every iteration?

    • @ZapOKill
      @ZapOKill 4 ปีที่แล้ว +2

      the worst case is where every substring is a palindrome... this only happens if you have always the same character, like "aaaaaaaaaaa"... then you have O(N²)

  • @arunprasanth9527
    @arunprasanth9527 3 ปีที่แล้ว

    sir now am clearly underStood
    actually i misunderstand that if statement bcs of that wiredMath Trick
    if am doing below code mean i can understand .tq soMuch sir.
    begin++;end--;
    if(resultLength

  • @kartikenbarnwal
    @kartikenbarnwal 3 ปีที่แล้ว

    I can listen to you the whole day !! 😍😍

  • @ngonimugandani4504
    @ngonimugandani4504 ปีที่แล้ว

    not sure if its the most efficient way of doing it but kinda works..
    public static boolean isPalindrome(String s) {
    if(s==""){
    return true;
    }
    String cleanedS = s.replaceAll("[^a-zA-Z0-9]", "");
    StringBuilder stringBuilder = new StringBuilder(cleanedS);
    StringBuilder reversedString = stringBuilder.reverse();
    if (cleanedS.equalsIgnoreCase(reversedString.toString())){
    return true;
    }
    return false;
    }

  • @dev-skills
    @dev-skills 3 ปีที่แล้ว

    I got this question asked in the first telephonic screening at google.

  • @nirmalm80
    @nirmalm80 4 ปีที่แล้ว +1

    Great explanation !!! please do more videos on leetcode sir

  • @aakashgupta7211
    @aakashgupta7211 ปีที่แล้ว

    What would be the Time & Space complexity for this algorithm? Isn't it still n^2?

  • @shashankjakhmola6226
    @shashankjakhmola6226 2 ปีที่แล้ว

    This code is not working for “bbbb” this type of string can you please help on this… what condition do we have to change so that with all possible scenarios it will work and this string also it will work?

  • @todortotev
    @todortotev 2 ปีที่แล้ว

    Great explanation, thank you!

  • @bthakur3870
    @bthakur3870 4 ปีที่แล้ว

    Hey Kaushik, Good work mate, In case strLength is 2, we can return same string, no need of further processing, Right ?

    • @umangjain5460
      @umangjain5460 4 ปีที่แล้ว +3

      No. What if the two characters are different? "ab" is not a palindrome. "aa" is.

  • @ruchibhadani6920
    @ruchibhadani6920 2 ปีที่แล้ว

    Hi koushik
    Great explanation but one of my test case is failed. I tried debugging and it is working fine while compiling but when I submit my test case I am getting index out of bound exception: begin 2 , end 8 , length 5 . input= rfkqyuqfjkxy output :r

  • @zoel.9537
    @zoel.9537 3 ปีที่แล้ว

    Can you create a channel for LeetCode challenge? Your explanation way better the Leetcode official solution.

  • @samahome
    @samahome 3 ปีที่แล้ว

    very good explanation to this bit tricky and complex problem

  • @harshitsaluja3809
    @harshitsaluja3809 3 ปีที่แล้ว +1

    Understanding the solution is not a problem but if every problem has its own way and it needs to be looked at like this how can someone come up with a solution at interview time with so much limited time frame unless they have already done it and know the solution.
    This is so demotivating.

  • @findarpit92
    @findarpit92 4 ปีที่แล้ว

    Awesome explanation. :) Thank you sir :)

  • @kaliraj5659
    @kaliraj5659 2 ปีที่แล้ว

    What if I reverse the String and Check the result with the original?.If the both are same. we will get a Palindrome.Is this way will provide a solution for the above Problem?

  • @mrOverdue
    @mrOverdue ปีที่แล้ว

    What about if palindrome substring is from 0 to n-2 or n-3 ... index or vice versa

  • @bhavinpatel5119
    @bhavinpatel5119 3 ปีที่แล้ว

    Very well explained. Really helpful

  • @GomboevRL
    @GomboevRL 4 ปีที่แล้ว

    Mr Koushik, your tutorial is wonderful. It amazing))). I stuck with one question about spring configuration. Can we reload spring properties in context dynamically without restarting applicatiom? And how to do it. Stack overflow with baeldung tells about Apache PropertiesConfiguration. But how to do it without boot. Only spring core?

  • @nathansnow
    @nathansnow 2 ปีที่แล้ว

    Or... Assuming we're looking for normal palindrome words, could we not just split the input string into an array of individual words, check and filter each word based on whether it's a palindrome, then just return the longest of all the found palindrome words?

  • @sachinmittal968
    @sachinmittal968 4 ปีที่แล้ว +1

    Sir, I am your big fan, I have asked you earlier also please please make a video on saga pattern in java. There is not a single tutorial on youtube implementating saga. Please make a video on that. I will be very thankful to you for this. Atleast mention the time like 6 months chlega

  • @salmankevin7205
    @salmankevin7205 4 ปีที่แล้ว

    Easy peasy after watching your video🤗. Why don't you post on competitive programming problems? 🙂

  • @snigdhamadhuri3133
    @snigdhamadhuri3133 4 ปีที่แล้ว

    Hi Koushik
    Can you please explain me the solution for this problem
    Given a list of three digit numbers as input write a function that prints unique combination and also their sum
    Input = [111 ,121, 131,111,141]
    output=[111:3 , 121:4 ,131:5, 141:6] the number after the colon is the sum of those 3 digits and it returned without any duplicates.
    Thanks,
    Snigdha.

    • @HanilKathuria
      @HanilKathuria 4 ปีที่แล้ว

      You can use HashMap for this

  • @pitchaiahbheemanapally2338
    @pitchaiahbheemanapally2338 3 ปีที่แล้ว

    What if given string is null ?
    Good explanation Kaushik.

  • @davinderpalsingh6412
    @davinderpalsingh6412 4 ปีที่แล้ว

    Nice explanation Kaushik.

  • @don300100
    @don300100 4 ปีที่แล้ว

    I have the same T Shirt as you are wearing Kaushik.... Thanks for the video :)

  • @dr.downvote
    @dr.downvote 3 ปีที่แล้ว

    Can someone please tell me why this single line code is not preferred to check if a string is palindrome or not????
    return new StringBuilder(s).reverse().toString().equals(s);
    we take a string builder and reverse the string and checks if it's same with given string, so why do we have to write our own loops if there is a default function which does this for us?

  • @sivasurendranath7734
    @sivasurendranath7734 ปีที่แล้ว

    How does this perform better than O(n^2) ? Arn't you still nesting loops ? Why would this be a better solution ? I don't get it.

    • @sivasurendranath7734
      @sivasurendranath7734 ปีที่แล้ว

      Oh i get it, its because we are not looping through the entire string again, rather just through the sub strings ?

  • @rahulsaxena9103
    @rahulsaxena9103 4 ปีที่แล้ว

    if the string has odd number of characters, do we still need to call 'expandRange()' twice? if yes, why? Can you please expand on that bit a little more. That's the only thing which seems a bit confusing.

    • @getrogo4474
      @getrogo4474 10 หลายเดือนก่อน

      same can some one explain

  • @ArjunKalidas
    @ArjunKalidas 4 ปีที่แล้ว

    I am still a little confused as to why we are calling expandRange method twice with different set of indices? I am not able to connect the dots. Can you please help me understand? Thank you!

  • @sarthaksrivastav3408
    @sarthaksrivastav3408 4 ปีที่แล้ว

    Awesome explanation. Please change your name from java to something else because I kept avoiding your channel because of java😂😂

  • @sanskritinannore7209
    @sanskritinannore7209 2 ปีที่แล้ว

    The follow up code of this for the leetcode question 5 would be:
    class Solution {

    int start=0;
    int end=0;
    int maxLen=0;

    public String longestPalindrome(String s) {
    if(s.length() < 2){
    return s;
    }

    for(int i=0;i=0 && last

  • @xilongzhang6856
    @xilongzhang6856 2 ปีที่แล้ว

    Hii I am fairly new to Java. i have a small question. Do we not need to instantiate the int we created? like int resultStart=0; int resultLength=0. when the condition 'resultLength < end-start+1' first appears, resultLength does not have a value...

    • @stephanbranczyk8306
      @stephanbranczyk8306 2 ปีที่แล้ว

      By default, the global instance variable resultLength is implicitly set to 0 when it's first declared I believe. But on Leetcode, you have to be careful with this because the test suite is not always well written. Sometimes, Leetcode will use the same Solution instance object to run through all of its tests, so that means that resultLength and resultStart might not get reset properly to 0 between tests. So if you want to be safe and avoid difficult to find bugs, if you're going to declare global instance variables within the scope of the Solution class, then you also need to explicitly initialize their value to 0 within the body of longestPalindrome() just to be safe.

  • @sithumdilanga650
    @sithumdilanga650 ปีที่แล้ว

    Thank you so much

  • @vaishnavi_cholli
    @vaishnavi_cholli 2 ปีที่แล้ว

    good explanation

  • @user-vx5sn9nw4u
    @user-vx5sn9nw4u 12 วันที่ผ่านมา

    I will do technical interview on Wednesday, hope i will got that type question

  • @senthilkumaran5317
    @senthilkumaran5317 3 ปีที่แล้ว

    Hello, can you tell us what is the Space|Time complexity of this solution please?

  • @ghanshyamkhatri3541
    @ghanshyamkhatri3541 4 ปีที่แล้ว

    U r great Kaushik!!!

  • @pranavchaudhary264
    @pranavchaudhary264 3 ปีที่แล้ว +1

    does he tell us what is run-time/space-time of his algorithm? is its n2 or linear?

    • @AmitToor
      @AmitToor 3 ปีที่แล้ว

      for (i=str.length,i>1;i--)
      for(j=0; j

  • @rmiliming
    @rmiliming ปีที่แล้ว

    Thank you for your nice video! There is sth confusing to me. For string "abc", when the loop starts from the first charcter "a", both the right and the left pointer will read from "a" first, but it seems that the "a" will be considered as a valid palindrome substring based on the method expandRange(). Am I wrong here?

    • @aakashgupta7211
      @aakashgupta7211 ปีที่แล้ว

      You are correct. Each individual character is a palindrome in itself.

  • @elukok
    @elukok 3 ปีที่แล้ว

    This was my first assignment on my first week on university and my first programming class. I remember being so overwhelmed by it and feeling like it is just impossible to do, now it is more of joke :D

    • @williaamlarsson
      @williaamlarsson 2 ปีที่แล้ว

      Damn, my first assignment was to get user input do some simple math and string processing and printing the result 😅 thats a harsh first assignment to get

  • @user-bd8nj4th8c
    @user-bd8nj4th8c 4 ปีที่แล้ว

    Hi Koushik, can you please do some spring integration videos for us.

  • @91-ritikjain36
    @91-ritikjain36 10 หลายเดือนก่อน +1

    I Watched all your video and i am not able to understand even an single question

  • @findarpit92
    @findarpit92 4 ปีที่แล้ว

    Please make videos of DP problems like this

  • @aasthachauhan4385
    @aasthachauhan4385 2 ปีที่แล้ว

    is the complexity oN2 right?

  • @chetanbhandari3033
    @chetanbhandari3033 4 ปีที่แล้ว

    Awesome, pls create video on time and space complexity

  • @you.tube12818
    @you.tube12818 4 ปีที่แล้ว

    I thought you mentioned that you would do Node JS videos daily .. eagerly waiting ..

  • @Aapla_Institute
    @Aapla_Institute 4 ปีที่แล้ว

    awsome , i am eagrly looking zuul api gateway to complet microservices .

  • @VikasKumar-wh2om
    @VikasKumar-wh2om 3 ปีที่แล้ว

    One point is not clear for me. Why are we calling expensdRange() method two times with different parameter? can we put these method in ifElse condition based on String odd/even length?

    • @Akshay-tj9hm
      @Akshay-tj9hm 3 ปีที่แล้ว

      As we don't have the start and end index, we don't know the string length in advance. Thats why we call it 2 times. As he says we move outwards, not inwards. I hope you get the point.

  • @shredder_plays
    @shredder_plays 4 ปีที่แล้ว

    Lovely great explaination do make a course on spring mvc , core

  • @dear_prit
    @dear_prit 4 ปีที่แล้ว

    When you are doing str.charAt(begin) == str.charAt(end) it will check the reference or the actual value? at 18:45 shouldn't we be using equals?

    • @pradeepwarade3669
      @pradeepwarade3669 4 ปีที่แล้ว

      actual value will be checked (there's no ref. variable), from .charAt() you get a char and not String

    • @abukasozi295
      @abukasozi295 3 ปีที่แล้ว

      the method charAt returns a character which is a primitive type/base type hence you can use == for evaluation, unlike reference types

  • @saddamahmad2310
    @saddamahmad2310 4 ปีที่แล้ว

    thank you very much sir for this video

  • @parambharti7095
    @parambharti7095 3 ปีที่แล้ว

    Awesome !!!!

  • @testingDhippiuh
    @testingDhippiuh 4 ปีที่แล้ว

    Thank you, sir.

  • @sheshashankar8304
    @sheshashankar8304 4 ปีที่แล้ว

    Hey! Thank you so much for the video. I have a doubt. Let us say that we have been given a string "madamefg". In the iteration where begin and end indices are 1, the while loop will be true and begin will become 0 and end will be incremented to 2. In the second iteration, the while loop will not be satisfied, as m != d. However, the if condition will be satisfied, as resultLength will be 0 and end - begin - 1 will be 1, and the resultLength and resultStart will be 0. But, mad is not even a palidrome. Could you please explain how this works?

    • @mirageman2
      @mirageman2 4 ปีที่แล้ว

      if you try it out with "mad" you get "m", because the substring has the indices 0 and 1 in the string, if you try "madam" the indices are overriten again with the provided palindrome of a greater length.

    • @nozzhard
      @nozzhard 3 ปีที่แล้ว

      "madam" it is a palindrome.

  • @malikwaseem1983
    @malikwaseem1983 3 ปีที่แล้ว

    You are awesome!

  • @noushadh1109
    @noushadh1109 4 ปีที่แล้ว

    @kaushik , How we know that , expandRange(...) got palindrome string ? This while loop anyway exit at one particular point either at end of index or characters are not equal. ?

    • @sheshashankar8304
      @sheshashankar8304 4 ปีที่แล้ว

      I have the same doubt. In some cases, after exiting the loop, the substring might not even be a palindrome, and yet we are storing the start index and the length only because the if condition gets satisfied.

    • @naveengs2001
      @naveengs2001 4 ปีที่แล้ว

      Yes, Even the for loop in the main method he wrote Is wrong , no need of -1there

  • @taslimapatel5960
    @taslimapatel5960 3 ปีที่แล้ว

    You are amazing !

  • @oluwarotimiadeola1997
    @oluwarotimiadeola1997 ปีที่แล้ว

    Is it just me or are we not allowed to reverse a string and compare it to check for a palindrome ....

  • @gomezvillegasdaniel
    @gomezvillegasdaniel 4 ปีที่แล้ว +1

    what about the special character avoidance? and what about that N^2 complexity, the for is calling a method with a while within

    • @ZapOKill
      @ZapOKill 4 ปีที่แล้ว

      the worst case is where every substring is a palindrome... this only happens if you have always the same character, like "aaaaaaaaaaa"... then you have O(N²)

  • @pandupvpk
    @pandupvpk 4 ปีที่แล้ว

    waiting for more LC..probably a collection...

  • @amargupta1728
    @amargupta1728 4 ปีที่แล้ว

    please make video on algorithm and data structure.

  • @momann4394
    @momann4394 2 ปีที่แล้ว

    9:50 - For those who already know the O(n^2) solution is bad

  • @sumansatpathy2862
    @sumansatpathy2862 4 ปีที่แล้ว

    Sir .. please make videos on spring 5 webflux

  • @ena81xx
    @ena81xx 2 ปีที่แล้ว +1

    Such a cheat!
    This is Nick White's solution.

  • @trungle-gn6bw
    @trungle-gn6bw 3 ปีที่แล้ว

    It's not a efficient approach, you can try to use Rolling-hash

  • @umangjain5460
    @umangjain5460 4 ปีที่แล้ว

    Correct me if I am wrong. The time complexity is O(n2) and space complexity is O(1).

    • @ZapOKill
      @ZapOKill 4 ปีที่แล้ว

      the worst case is where every substring is a palindrome... this only happens if you have always the same character, like "aaaaaaaaaaa"... then you have O(N²)
      I would argue that space complexity is the O(N), since you still need to String, I guess you are referring to Auxiliary Space which is O(1), since you only need constant amount of variables and no string duplication.
      However there are faster Algorithms running in O(N) time, but with higher space complexity... Boyer-Moore string-search algorithm can be modified, and suffix trees also can solve the problem faster

    • @umangjain5460
      @umangjain5460 4 ปีที่แล้ว

      @@ZapOKill Yeah, I meant auxiliary space complexity.

  • @vikashtiwari2136
    @vikashtiwari2136 3 ปีที่แล้ว

    For smallest palindrome substring?