Leetcode 5. Longest Palindromic Substring

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ธ.ค. 2024

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

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

    Code CPP:
    int n =s.size() , maxlength = 0; string ans;
    vectordp(n,vector(n,0));

    for(int diff = 0;diff

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

      #define f(a,b) pair temp = func(s,a,b);if(temp.second-temp.first+1>ma){ma = temp.second- temp.first+1;x=temp.first;y=temp.second;}
      class Solution {
      public:
      pair func(string &s, int i, int j)
      {
      while(i>=0 && jma){f(i,i);}
      i=q+k;
      if(ima ){f(i,i+1);}
      if(min(2*i+1,2*(n-i-1)+1)>ma){f(i,i);}
      }
      return s.substr(x,y-x+1);
      }
      };
      //Without DP : Runtime : 3s | Space : 7.1 MB
      //intuition : go thru every index and search for even palindrome and odd palindrome + maintain longest length so u dont have search in those subarrays which can't get upto maximum

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

      Please first explain its recursive/memoized version then move to Bottom Up Approach

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

      hi , im not able to understand this vectordp(n,vector(n,0)); can u pls explain this . im stuck in there 😢

    • @AshishKumar-ny4cq
      @AshishKumar-ny4cq ปีที่แล้ว +1

      Its says memory limit exceeded

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

      @@wondersofmovies we are making a 2d dp table of vector of vectors where in the bracket the n signifies the number of rows and each col is also a vector of size n and we initialise al values with 0.

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

    If possible make a playlist covering all DP concepts that would genuinely help because you teach very well.

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

    based on my analysis what I've come across is, if it's dp with string better try with tabulation itself rather than recursion ->memo ->tabulation. It will help you a lot in solving problem and building intuition. Thanks for the tutorial Alisha!!

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

    I haven't even started DP as of now, but I was solving the Data Structures II of leetcode where I came across this question and now after understanding the concept you explained, it's so clear to me!!! Thank you so very much!!!!

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

    Dry run and code explanation is just outstanding

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

    Thank you so much. This video helped me a lot.
    Python version :
    n = len(s)
    dp = [[0]*n for j in range(n)]
    res = ''
    resLen = 0
    for diff in range(n):
    for i in range(n):
    j = i + diff
    if j >= n:
    break
    if diff == 0:
    dp[i][j] = 1
    elif diff == 1 and s[i] == s[j]:
    dp[i][j] = 2
    elif s[i] == s[j] and dp[i+1][j-1]:
    dp[i][j] = dp[i+1][j-1] + 2
    if dp[i][j]:
    curLen = j-i+1
    if curLen > resLen:
    resLen = curLen
    res = s[i: j+1]
    return res

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

    Spent whole day on this , Thanks for each minute !

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

    Your explanation is very smooth.

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

    Thank You Alisha for this awesome video,
    You explained complex things in a very easy manner,
    May god bless You,
    Keep it up

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

    The way you explain its just another level every problem becomes easy.. 😇

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

    Great explaintion yet again, now addicted to your channels for explainations/logic😁 Tagging a **two pointer approach** for the same problem : 👇 class Solution
    {
    public:
    string longestPalindrome(string s)
    {
    int maxLengthOfPalindrome = 0;
    int maxStringStart = 0;
    int right , left;
    int lengthOfString = s.size();
    int lengthOfPalindrome;
    string ans = "";
    for(int i = 0 ; i < lengthOfString ; i++)
    {
    right = i;
    while(right < lengthOfString and s[i] == s[right]) right++;
    left = i - 1;
    while(left >= 0 and right < lengthOfString and s[left] == s[right] ) left--,right++;
    lengthOfPalindrome = right - left - 1;
    if(lengthOfPalindrome > maxLengthOfPalindrome)
    {
    maxLengthOfPalindrome = lengthOfPalindrome;
    ans = s.substr(left+1,maxLengthOfPalindrome);
    }
    }
    return ans;
    }
    };

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

    understood everything didi.Keep up the good work😊😊.I have a suggestion in the code...instead of calculating substring again and again we can just keep track of start and end point of substring and then while returning we can return in subtring format using those indexes .it will reduce time complexity to calculate the substring
    here code for my explaination
    public String longestPalindrome(String s) {
    int n = s.length();
    int dp[][] = new int[n][n];
    int startIdx = 0;
    int endIdx = 0;
    int maxLength = 0;
    //diagonally iterating
    for(int diff=0;diff0){
    if(j-i+1>maxLength){
    startIdx = i;
    endIdx = j+1;
    maxLength = j-i+1;

    }
    }
    }
    }
    return s.substring(startIdx,endIdx);
    }

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

    i have already done the question ....then also i came so that i can like ur video..thank for uploading video..keep going🤗🤗

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

    just to add, check how substring method in ur language works. in some languages it takes length, in some others, it takes end index which can also be inclusive or exclusive.
    in java, use : substring(i, j+i) as it takes start index and end index where end is not inclusive.
    BTW, kudos for the explanation, couldn't be any simpler !

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

    those who are facing problem in java code and thanks for such a great explanation 🙂
    public String longestPalindrome(String s) {
    int n = s.length();
    int dp[][] = new int[n][n]; int max = 0; String ans = "";
    for(int diff = 0; diff len = 2-0+1 = 3
    */
    int len = j - i + 1;
    if(len>max){
    max = len;
    ans = s.substring(i,j+1); // substring start from i and end at j so j+1
    }
    }
    }
    }
    return ans;
    }

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

    You are a great explainer Alisha.....Thanks for the video

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

    Bhai kya hi samjhaya hai, sab samajh aa gya. Thanks for posting it. 🧡 from remote.

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

      Iska Time Complexity ky rhega bro?

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

      ​@@aakashbhandari9761 TC O(n2)
      SC O(n2)

  • @bhavnayadav5933
    @bhavnayadav5933 6 หลายเดือนก่อน

    woow di ,such a great explanation,thanku for helping me ,i have read dp for the first time,but u explained each concept so well that i understood in go

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

    Thanks a lot !! You made DP so easy ...I always found this so confusing until I came to your channel :) .

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

    Your explanation is amazing. The suggestion is to cover some questions from GKG too. Thank you for nice content

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

    Woww!!! That’s amazing!! So easy to understand!!

  • @KhushalGautam-ne5nc
    @KhushalGautam-ne5nc 9 หลายเดือนก่อน

    1000th like. I always search for your videos whenever I get stuck at any question. You are my tech crush❤

  • @Sumit-ef3tu
    @Sumit-ef3tu 4 หลายเดือนก่อน +1

    helpful👍

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

    your energy of explanation way is incredible🥰...

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

    The best explanation on YT..

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

    Thankyou mam, Your teaching is amazing. Keep it up.

  • @djpro6413
    @djpro6413 6 หลายเดือนก่อน

    Hi were you learn from this all the stuffs and how you go through the problems and solve it can you exaplain

  • @princekumar-ot1ul
    @princekumar-ot1ul ปีที่แล้ว

    very well explained but I think little bit modification is required because it is not working for input (s="cbbd")

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

    Thankyou alisha very informative tutorial

  • @PalakGrover-q1u
    @PalakGrover-q1u 8 หลายเดือนก่อน

    Very amazing explanation!

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

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

    your concept is very good.thanks .

  • @PujaBende-bn1ws
    @PujaBende-bn1ws 4 หลายเดือนก่อน

    The explanation is really great. Thanks for the video. But this is not Manacher's algorithm which can solve it in O(n). Do you have that explanation?

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

    thanku ma'am for very clear explanation

  • @rishabhvaish835
    @rishabhvaish835 6 หลายเดือนก่อน

    such a great explanation, thanks

  • @VV-ud3nh
    @VV-ud3nh 5 หลายเดือนก่อน

    Best Explanation !

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

    Awesome explanation

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

    Loved your explanation please make more videos keep up the good work. If possible you can try to cover one of the DSA sheet of striver or love babbar.

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

    Perfect explanation

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

    Very well. Explained thnx a lot

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

    This is very easy explanation for this question

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

    Concise and Clear. Thanks

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

    outstanding explanation mam ,i reallly loved it .......

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

    great work alisha

  • @startercoder
    @startercoder 9 หลายเดือนก่อน

    Java code -:
    class Solution {
    public String longestPalindrome(String s) {


    int end=s.length();
    String ans="";
    int maxLen=0;
    int dp[][]=new int[end][end];
    for(int arr[]:dp){
    Arrays.fill(arr,0);
    }

    for(int diff=0;diff

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

    Can we rename the variables i and j so that we can understand what those are even we look after years

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

    Great Explanation !!

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

    Great explanation. Period.

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

    Nice explanation:)

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

    Daymmmm, that was do much helpful. Thank you!

  • @projectsdb4034
    @projectsdb4034 9 หลายเดือนก่อน

    what about the remaining empty boxes in matrix? will not iterate?

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

    alisha mam,
    for this code time complexity o(n)?

  • @samrat_malisetti
    @samrat_malisetti 7 หลายเดือนก่อน

    Need explanation about recursive brute force approach then try to explain tabulation. Directly dive into tabulation will make confusion in DP.

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

    Referred so many solutions but no help. Loved the fact how lemon squeezy you made this ques look like 😅😅😅😅

  • @VivekKumar-xx5fy
    @VivekKumar-xx5fy 2 ปีที่แล้ว

    very very easy and helpful🔥🔥

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

    I feel like i am really dumb. I understand the solution when i watch her video then don't remember it after some time . Its frustrating . Someone plz suggest some tips

  • @aryanakash367
    @aryanakash367 9 หลายเดือนก่อน

    nice explanation

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

    Great explanation 😊😊

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

    its your video i search for when i search any Q because your teaching style is very easy to understand i had problem in understanding tabulation but your 1 solved Q made it clear which the whole playlist of others couldnt do

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

    Thanku🙏🙏🙏🙏

  • @Idukhan-jj9kc
    @Idukhan-jj9kc 2 ปีที่แล้ว

    Best solution 👌

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

    can you explicitly write the complexity also in every code

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

    Absolutely perfect

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

    Nice explanation 😊

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

    It shows memory limit exceeded in interviewbit

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

    which whiteboard application is used?

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

    Nice Explanation . Is there any possibility that can you suggest any website for aptitude preparation and Guesstimate?

  • @vish-singh
    @vish-singh 2 ปีที่แล้ว

    very well explained👏

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

    great explanation

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

    can you show non-dp approach for O(1)

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

    Nice explanatuon

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

    Amazing explanation

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

    The method is not applicable to java..
    Could you provide why it is?

  • @vanshvermadtu
    @vanshvermadtu 3 หลายเดือนก่อน

    Excellent

  • @ARkhan-xw8ud
    @ARkhan-xw8ud 9 หลายเดือนก่อน

    thanks

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

    it helps a lot to understand ,Thank you so much for your contribution.but can we make it O(n) somehow??

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

    Awesome 🤯👍

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

    Time Limit Exceeded (TLE) error aa raha didi leetcode per ye code submit krne pe

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

    Thank You!!

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

    Great !

  • @md.imrankhan6912
    @md.imrankhan6912 ปีที่แล้ว

    Great

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

    mam baba is also plindrom substring so why it is not in ans?

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

      No ! baba is not pallidrome when you read the string from back it will be abab which not equal to original one

    • @finder1013
      @finder1013 11 หลายเดือนก่อน

      ​@@aakashbhandari9761thank you for reply doubt clear

  • @Sri_9991
    @Sri_9991 23 วันที่ผ่านมา

    better than Nick

  • @mineekumari7035
    @mineekumari7035 6 หลายเดือนก่อน

    not able to understand this code

  • @GauravSharma-ry7yc
    @GauravSharma-ry7yc 10 หลายเดือนก่อน

    Your explanation is great but try going a bit slow to let it sink in for the viewers :)

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

    Thank you

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

    Have u studied chemistry from JMR sir in resonance.

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

    thankyousomuchh

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

    thanks girl

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

    Amazing Explaination 🤩

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

    Miss Alisha Kabhi apne dill mein bhi welcome kar lo hamesha youtube channel pe welcome karti ho 🙂🙂🙂

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

    Genius

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

    Mam I tried your approach in java on leetcode but only 12 out of 140 test cases has paassed. Can you help me find the error in the code.
    Code:
    class Solution {
    public String longestPalindrome(String s) {
    int n=s.length();
    int[][] dp = new int[n][n];
    String ans="";
    int max=0;
    for(int diff=0;diff

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

      TRY THIS!!!
      public String longestPalindrome(String s) {
      int n=s.length();
      int[][] dp=new int[n][n];
      String ans="";
      int maxlen=0;
      for(int diff=0; diff

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

      The bug is
      ans=s.substring(i,max); is wrong.
      it should be ans=s.substring(i,j+1);
      Explanation: when we are updating the max it means we have the matrix indices which are nothing but string indices.

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

    love you mam

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

    Where r u @dhananjoy

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

    why are u in so much hurry?

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

    mujhe samaj nhi ata jab palindrome ,ye sb software development me kam nhi ata to padhate aur puchhte kyu h isse question

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

      same goes with Aptitude Tests before interview rounds...may be they want to filter some students based on their logical and coding ability...BUT truth is this is not that much helpful in Development..

  • @ChristForAll-143
    @ChristForAll-143 ปีที่แล้ว

    will you marry me? how come u so good with explanation and graceful?
    been following from so long❤
    great, but please don’t post more and more problem just give core conepts in a single vide like nge, nse, bfs/dfs/disjointset/swapping technique etc

  • @afzal.q842
    @afzal.q842 2 ปีที่แล้ว +1

    I need ur another social media I'd need to talk with you i am not on LinkedIn i need to share some content to you .

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

      Send here yr useful content

    • @afzal.q842
      @afzal.q842 2 ปีที่แล้ว

      @@shimlamam6066 give me ur insta id

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

      @@shimlamam6066 tomb tanane ka tlueprint dera hoga

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

    Very Great Explanation 🔥