Manacher's Algorithm | Longest Palindromic Substring

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ก.ย. 2024
  • In this video I will be discussing Manacher's algorithm which is used to find the longest palindromic substring in linear time. Its a fairly complex algorithm and understanding its time complexity is the hardest part. So, in this video I will help you understand it to the fullest.
    Practice problems :
    Easy - www.codechef.c...
    Hard - codeforces.com...

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

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

    can not understand the psudo code in over 2 times watching this, I am ok with O(n)2.

  • @Lime-rr6te
    @Lime-rr6te 4 ปีที่แล้ว +5

    this was the best manachers algo explanation i found, better that tushar roy, tech dose, nick white
    and so many others

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

    Case 2 shown during 13:13 is wrong. p[i] >= p[i'] could be false, the palindrome centered in s_i' could extend back far beyond s_l. The conclusion that p[i] must be greater or equal to r-i is right, however.

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

      Yes, Case 2 is wrong, zxaxz#zxaxp , where i = index of 2nd a

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

    rather than adding hashes, you can just compare i,i (i being the centre). and make a function for it and fn(i,i) for odd, fn(i,i+1) for even.

  • @user-coder-r6j
    @user-coder-r6j หลายเดือนก่อน

    Thank you a lot. Only this video deeply explained an algorithm!

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

    Too good. Thanks for explaining it in so simple terms and also for covering every case.

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

    this is the best video among all the videos on youtube explaining manacher's algorithm

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

    //c++ code
    class Solution {

    public:
    string getModifiedString(string s, int n){
    string sb="";
    for(int i = 0; i < n; i++){
    sb.append("#");
    sb+=s[i];
    }
    sb.append("#");
    return sb;
    }
    public:
    string longestPalindrome(string s1) {
    int m=s1.size();
    string s=getModifiedString(s1,m);// expand the string for even expansion of string
    cout

  • @dhruvsingh5190
    @dhruvsingh5190 8 หลายเดือนก่อน +2

    best explanation found till now for this algorithm.🔥🔥

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

    very clear and comprehensive video. The best explanation for Manacher's Algorithm!

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

    Well Explained! Among all the videos about Manacher's Algorithm, your explanation is best. Thank you for making this video.

  • @RAJPATEL-nm9nz
    @RAJPATEL-nm9nz 4 ปีที่แล้ว +1

    Only one doubt why you have written, If(s[i-k]!=s[i+k]) --k

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

    Great Video. Thanks for the simple and intuitive explanation!

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

    I think the algorithm will fail for a test case such as "cbbd"....

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

      So let’s see what happens here
      We rewrite your test case into
      # c # b # b # d #
      And then we simulate the algorithm…
      I will do it manually…
      i = 0
      l = 0, r = -1
      i is bigger than r, k = 0
      We expand k to 1 and cannot further due to the left bound limitation, k = 0 again, p[0] = 0
      We extend l to 0 and r to 0
      i = 1
      i is bigger than r again so k = 0
      We push k to 2 and it fails due to mismatch
      k is 1 then
      p[1] = 1
      We extend l to 0 and r to 2
      i = 2
      i is equal to r so we pass to else
      j is equal to 0, thus k starts at 0
      We run the while loop and it fails at k = 1 due to mismatch then k = 0
      p[2] = 0, l = 2 and r = 2
      i = 3
      i is bigger than r, so k = 0
      After while it fails at k = 2 so k is 1, so p[3] = 1 and l = 2, r = 4
      i = 4
      i isn’t bigger than r
      j = 2, so p[j] = 0, thus k becomes 0, it fails for k = 3 so k = 2, p[4] = 2, l = 2, r = 6
      i = 5
      i isn’t bigger than r
      j = 3, p[j] = 1, k = min(1, 6-5)
      we start at k = 1, it fails at 2 so k stays at 1
      p[5] = 1
      l = 4, r = 6
      i = 6
      i isn’t bigger than r
      j = 4, p[j] = 2, so k is min(2, 0)
      k fails for 1 due to mismatch and k = 0, so p[6] = 0, l = 6, r = 6
      i = 7
      i is bigger than r so k = 0
      it fails at k = 1 cause further it cannot extend, p[7] = 1, l = 6, r = 8
      i = 8
      i isn’t bigger than r so j = 6, k = min(0, 0) and then fails due to the range, so p[8] = 0, l = 8, r = 8, the end of algorithm, let’s see what’s the outcome…
      # c # b # b # d #
      0 1 0 1 2 1 0 1 0
      Seems correct to me

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

      @aishwaryastogi7265

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

    There is a little bug in the code, the --k step after expansion is mandatory.

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

    Can you also consider making a video on Ford Fulkerson max flow algorithm??

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

    All of your videos are just amazing !
    Please make more videos...!
    Thanks a lot !

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

    Nice explanation!

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

    Best explaination on the internet

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

    Great explanation brother.....

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

    Just watching first 4 mins and I found its very fluent and easy to understand. 🙏

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

    greatest explanation of this algorithm on the internet.

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

    Great explanation, thank you!

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

    Can you make a video on sweep line algo as a whole?

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

      I'll consider it. For now you can refer www.topcoder.com/community/competitive-programming/tutorials/line-sweep-algorithms/

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

    good explaination

  • @電腦騙徒剋星
    @電腦騙徒剋星 ปีที่แล้ว

    one of the youtuber that can explain manacher pretty well , respect

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

    Brother why did u leave making videos.
    They are all exceptional..

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

      I will even subscribe ur channel.now
      Plz keep on uoloading

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

    Nice video

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

    Really good video 🙂🙂🙂

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

    Thank you. It is very well explained.

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

    bhai pseodo code link wagera de diya karo

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

    Best !!!!!!

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

    I want to ask one thing that does this code only work for showing odd length substrings or will it work for showing even length palindromic substrings also.
    I tried for the string "bbbb" on this code and this code is not working.
    After seeing many videos, I finally understood Manacher's algorithm seeing this video. Thanks a lot and hoping to see more videos from this channel like this.
    Edit:
    One more thing I would like ask is this condition
    if(S[i-k]!=s[i+k]) required as I am facing problem with that line of code and finally removed and your code is running perfect for finding odd palindromes only.
    Once again Thanks for amazing explanation of concept.

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

    great!

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

    Bro your videos are literally awesome.

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

    Really great explainaion

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

    Can u make a solution video for the PALIN3 problem .. I am unable to understand the precomputation required in it... thanks

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

      You can checkout my explanation here : discuss.codechef.com/t/palin3-editorial/4906/6?u=vedhant

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

      @@fluentalgorithms4847 " To find ans[i], ans[i] = cnt[i+k][s[i]%3] - cnt[i][s[i]%3] will not work because sum[i] would have contributed to the sum right of I "
      Can u pls explain this line once? thanks

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

      I have updated my explanation here : discuss.codechef.com/t/palin3-editorial/4906/6?u=vedhant

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

      @@fluentalgorithms4847 Thanks a lot ... finally understood :)

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

    Thank you, your video helps a lot.

  • @Rohitkumar-ks9io
    @Rohitkumar-ks9io 4 ปีที่แล้ว

    Please provide a problem link related to this algorithm . you can provide it in description for further videos.

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

    best explanatiion!!

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

    Good explanation

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

    ❤❤❤

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

    Great video, but try to make video at max 10 minutes it will help to retain viewers, because everyone is searching for solution in short time.

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

    brilliant explanation sir ! , thanks a lot