Rabin Karp - Shortest Palindrome - Leetcode 214

Поделиться
HTML-код
  • Опубликовано: 14 янв 2025

Комментарии • 99

  • @DenisYordanov-co3qg
    @DenisYordanov-co3qg 3 месяца назад +40

    We love you too, man, bought your courses for the next 1 year and now I am sorry I did it ( should have bought the unlimited access ), your courses ( and the upcoming ones ) are way too good. At least I’ll get to pay for the courses twice, you deserve it!

  • @NeetCodeIO
    @NeetCodeIO  3 месяца назад +26

    8:44 - Correction, the prefix of length 3 doesn't match suffix of length 3, i mispoke

    • @professorpoke
      @professorpoke 3 месяца назад +1

      Everyone makes mistakes

    • @adilansari-hq9ge
      @adilansari-hq9ge 3 месяца назад +2

      Bro, overr the years your teaching style has improved a lot. Can you re-record the important Algo like KMP.

  • @cineaddict1633
    @cineaddict1633 3 месяца назад +22

    Bruh you're the reason I'm solving daily challenges these days

    • @OmarAlimammadzade-v7s
      @OmarAlimammadzade-v7s 3 месяца назад +2

      I hope we can finish September LeetCoding challenge with this channel.

  • @studyaccount794
    @studyaccount794 3 месяца назад +8

    Intro put a smile on my face. Haven't heard that in a while because of the interview grind. Love you too Navdeep 🤝🤝

  • @prameydongre
    @prameydongre 3 месяца назад +19

    love you too, neet!

  • @MP-ny3ep
    @MP-ny3ep 3 месяца назад +8

    Honestly , Leetcode should make you their editor-in-chief

  • @business_central
    @business_central 3 месяца назад +3

    We enjoy these videos too! Hope you never stop making them! Thank you very much!

  • @prajwals8203
    @prajwals8203 3 месяца назад +21

    0:06 what was that?? I feel a storm coming
    rabin karp >>>>>>> kmp
    I lost last of my brain cells understanding kmp
    21:45 yep....thanks for making our life easier by uploading these videos

  • @SidhanthSanil
    @SidhanthSanil 3 месяца назад +7

    love you too navi! thanks a ton buddy

  • @Tyheir
    @Tyheir 3 месяца назад +1

    I enjoy the random reaction videos you do because you provide intuitive insight. You should continue doing them

  • @JaredBru
    @JaredBru 3 месяца назад

    Love you too neet for being here I think it’s been helpful for me that your here and your the reason I actually try to learn leetcode as a beginner in programming for two months and hopefully I’d get to enjoy and build some good foundation for myself in the future

  • @akialter
    @akialter 3 месяца назад

    I memorized Rabin Karp in college. Still remembered being asked that in final and luckily memorize the whole formula. Your explanation blew my mind how simple it was. Thank you

  • @pastori2672
    @pastori2672 3 месяца назад

    honestly in the kmp algorithm vid, the dual screen was actually great, i hope you use it for future problems as well so we don't forget which part is which in the explanation, like code and explain as you go

  • @Philgob
    @Philgob 3 месяца назад

    we love you bro these videos are insane quality even if they get less views please keep at em 🙏🏽

  • @roviaasucksateverything501
    @roviaasucksateverything501 3 месяца назад +2

    1 doubt, in 14:52 you've written 1,1 at the top and 1,1 for the bottom string then why don't you consider that as a pallindrome.

  • @fancypants6062
    @fancypants6062 3 месяца назад

    I am really glad that you enjoy making these, because I have learned a shitload from you.

  • @godoftroller4825
    @godoftroller4825 3 месяца назад +1

    stuck in the test case and waiting for u :))) i love neetcode

  • @UdayKiran-zd3lr
    @UdayKiran-zd3lr 3 месяца назад +2

    ❤ Navdeep Singh

  • @nikhil199029
    @nikhil199029 3 месяца назад

    casual way of presenting hard problems

  • @pastori2672
    @pastori2672 3 месяца назад +2

    so sad these lc videos don't get nearly as many views 😞

  • @freecourseplatformenglish2829
    @freecourseplatformenglish2829 3 месяца назад +1

    We love you too ❤🧡💛💚💙💜🤎🖤🤍

  • @cakesama9770
    @cakesama9770 3 месяца назад +1

    theres no way you could come up with the second solution on your own without help unless your a genius

  • @Aditya-vg2lz
    @Aditya-vg2lz 3 месяца назад

    actually I tried to explain the KMP algorithm in an interview , and it didn't go well
    the best would be to use the inbuilt find function for strings or use the rabin karp algo

  • @romannikiforov5122
    @romannikiforov5122 3 месяца назад

    Another way, I think, is to use Manacher’s Algorithm to find longest palindromic substring. But it doesn't look easier or more intuitive than KMP.

  • @bishwashkumarsah171
    @bishwashkumarsah171 3 месяца назад +3

    I still don't get it. Why prefix * base but for suffix its char * power 😭. Wasted whole day still couldn't get it.

  • @swatiswagatika2017
    @swatiswagatika2017 3 месяца назад

    Thanks for explaining the rolling hash . Before watching this video i struggled a lot to understand the concept. :)

  • @sarveshhiwase193
    @sarveshhiwase193 3 месяца назад

    Hey Neetcode, just one suggestion,
    would you like to make videos on general algorithms and patterns which are long in length, but really deep dives into them and give intuition behind those.
    These problem solution videos also really helps,
    but if you can generalize these algorithms videos it would be really helpful.
    Thanks a lot.

  • @OmarAlimammadzade-v7s
    @OmarAlimammadzade-v7s 3 месяца назад

    Thanks for everything, teacher.

  • @karthik_sama
    @karthik_sama 3 месяца назад

    This is a brilliant explanation. Glad i watched it.

  • @adityag6022
    @adityag6022 3 месяца назад

    Thank you Neetcode. Love you too :)

  • @kushagrakartik360
    @kushagrakartik360 3 месяца назад

    Isn't what you're describing at 13:38 left shift and not right shift?

  • @deepakjain4481
    @deepakjain4481 3 месяца назад

    i am trying so hard learnt dsa and then got to leetcode i feel so demotivated when i am not able to solve a problem it feels like something is not right

  • @SwapnilDeshpande
    @SwapnilDeshpande 3 месяца назад

    Love you too Neet!

  • @jitpatel1105
    @jitpatel1105 7 дней назад

    You r awesome,Thank You

  • @tylerbarnes5087
    @tylerbarnes5087 2 месяца назад

    i spent like 2 hours trying to tweak manachers algorithm wish i had watched this before

  • @kaushalvarma-e4j
    @kaushalvarma-e4j 3 месяца назад

    Great insights into problems, thank you.

  • @yashwantmoharil5892
    @yashwantmoharil5892 3 месяца назад

    Are you always able to come up with a solution on your own?

  • @karthi7102
    @karthi7102 3 месяца назад

    Thank you for making the video, please make video for every daily problems everyday

  • @muffincodingchannel
    @muffincodingchannel 3 месяца назад

    Such a nice solution.

  • @chukkalasaiganadev79
    @chukkalasaiganadev79 3 месяца назад

    ❤you and your videos Neet !

  • @jonas-ke4qz
    @jonas-ke4qz 3 месяца назад

    It would be super neat if you gave a "pause the video" moment for the watcher to try to come up with the intuition before answering. Great video as always

  • @Vegeta-gb2pm
    @Vegeta-gb2pm 3 месяца назад

    Love you too man ...

  • @andrefreitas9936
    @andrefreitas9936 3 месяца назад

    the famous rolling hashes🥱🍃

  • @vishwafuru
    @vishwafuru 3 месяца назад

    love you too, neet.

  • @mageshyt2550
    @mageshyt2550 3 месяца назад

    find largest palindromic prefix and then adding the reverse of the remaining suffix in front to make the entire string a palindrome in the shortest way possible.
    class Solution:
    def shortestPalindrome(self, s: str) -> str:
    n=len(s)
    if n=0:
    # find the longest palindrome from the start
    if s[l] == s[r]:
    print(f"MOVING ({l},{r}), ",s[l],s[r])
    l+=1
    r-=1
    if l==n:
    return s
    suffix=s[l:]
    prefix=s[:l]
    print("[PRFIX,SUFFIX]",prefix,suffix)
    return suffix[::-1]+self.shortestPalindrome(prefix)+suffix

  • @Gojo-hl7iu
    @Gojo-hl7iu 3 месяца назад

    Thank you for making these videos everydat

  • @karthi7102
    @karthi7102 3 месяца назад

    Great explanation

  • @rohitkumaram
    @rohitkumaram 6 дней назад

    I get it when you say:
    people who think they understand KMP are fooling themselves, I was one of those.
    Doing it through KMP took me forever:
    ```
    class Solution:
    def shortestPalindrome(self, s: str) -> str:
    preLPS, i = 0, 1
    ss = s+'#'+s[::-1]
    lps = [0]*(len(ss))
    while i

  • @mannishkarki
    @mannishkarki 3 месяца назад

    thanks as usual. Any intuition behind using base 29, you definitely mentioned at least 26, but wondering if there's anything that went behind?

  • @Gojo-hl7iu
    @Gojo-hl7iu 3 месяца назад

    we love you tooo!!

  • @100nitishyadav8
    @100nitishyadav8 3 месяца назад

    I am new to leetcode can you tell me These algorithms like rabin karp, kmp all these do we need to remember them in order to actually get good at leetcode ? Does your main channel have any playlist including all these type of algorithm sliding window and etc which are most commonly used in interview and like 80/90% of leetcode questions can be solved with few main important algorithm something like that, can you recommend something like that ?

  • @sheikhmkrifat7749
    @sheikhmkrifat7749 2 месяца назад

    It would be better for us if you just code the brute Force approach , cause sometimes i failed to code the bruteForce solution by myself

  • @prasunroy2802
    @prasunroy2802 2 месяца назад

    Love you too bro

  • @sazsevag
    @sazsevag 3 месяца назад

    We love you too

  • @VanjeAv
    @VanjeAv 3 месяца назад

    WE LOVE YOU TOO

  • @AMX0013
    @AMX0013 День назад

    noob question, if hashing/
    encoding iss the way, cant we use a hashmap directly? keys are always hashed?

  • @sirk3v
    @sirk3v 3 месяца назад

    much love mr neet

  • @AlfredPros
    @AlfredPros 3 месяца назад +2

    A slight optimization to make the brute force approach not timed out is to only loop from the center of the string back to index 0.
    for i in range(len(s)//2, -1, -1):
    # Check if palindrome
    The reason we start from the center is because beyond the middle, no longest palindrome will reach index 0. This saves time enough to pass the time limit.

  • @Aanand-dd1kj
    @Aanand-dd1kj 3 месяца назад

    GOOD EXPLANATION

  • @PreethamC-w4p
    @PreethamC-w4p 3 месяца назад

    bro can you make a video on morris traversal

  • @LasTCursE69
    @LasTCursE69 3 месяца назад

    Nice one

  • @guy_cod
    @guy_cod 3 месяца назад

    Hello Neetcode
    Do you think checking is palindrome using two pointer technique is more effecient that checking s == s[::-1], because the first solution giving you TLE, passess all test cases if you check is palindrome using s == s[::-1]. That is what I noticed when I bruteforced mine

    • @_PINAKIBANERJEE_CSEA
      @_PINAKIBANERJEE_CSEA 3 месяца назад

      doing s[::-1] creates new copy.. so for sure two pointer is better

    • @guy_cod
      @guy_cod 3 месяца назад

      @@_PINAKIBANERJEE_CSEA thank you, exactly what I thought I don't know why leetcode says time limit exceeded for two pointer, but not for s[i] == s[::-1]

  • @romanivanov6183
    @romanivanov6183 3 месяца назад

    Luv u 2 man

  • @tanishqvarshney5309
    @tanishqvarshney5309 2 месяца назад

    8:47 how does "aac" match "caa"?

    • @obscureautumnfrost
      @obscureautumnfrost 7 дней назад

      They match when you read aac left to right and caa right to left.

  • @redomify
    @redomify 3 месяца назад

    I love u neetcode

  • @arjunvasir2557
    @arjunvasir2557 3 месяца назад

    can we use a stack?

  • @BehavioralFallacy
    @BehavioralFallacy 3 месяца назад

    It's not I am watching this video because of lack of intuition in leetcode. I am watching this because I respect what you are doing and supporting programmer like me. Just kidding: I don't have leetcode premium. Poor me :(

  • @limsiryuean5040
    @limsiryuean5040 3 месяца назад

    love you too :)

  • @sharathkumar8338
    @sharathkumar8338 3 месяца назад

    i'v lost all the motivation after gpt o1

  • @kapilkhandelwal48
    @kapilkhandelwal48 3 месяца назад

    I think KMP is easier than this I solved this question using that only or may be I just don't want to learn any more complex string matching algorithm.
    My c++ code:
    class Solution {
    void lpsFind(string &s , vector & lps)
    {
    int pre = 0 , suff = 1;
    while(suff < s.size())
    {
    if(s[pre] == s[suff])
    {
    lps[suff] = pre+1;
    pre++;
    suff++;
    }
    else
    {
    if(pre == 0)
    {
    lps[suff] = 0;
    suff++;
    }
    else
    {
    pre = lps[pre - 1];
    }
    }
    }
    }
    public:
    string shortestPalindrome(string s) {
    int n = s.size();
    string rev = s;
    reverse(rev.begin(), rev.end());
    string sNew = s + "#" + rev;
    int nNew = sNew.size();
    vector lps(nNew, 0);
    lpsFind(sNew , lps);
    return rev.substr(0 , n - lps[nNew-1]) + s;
    }
    };

  • @shreyash184
    @shreyash184 3 месяца назад

    in brute force approach, axxxb here lPS is xxx so according to your approach it should be baxxxb, but answer is bxxxaxxxb, can you please explain this ?

  • @akashverma5756
    @akashverma5756 3 месяца назад

    I have tried to understand KMP three times but failed.

  • @kthamim7552
    @kthamim7552 3 месяца назад

    i dont understand why 29

  • @venkatgaddam6587
    @venkatgaddam6587 3 месяца назад +1

    0:06 Huh, is the problem that hard
    Or you up to something ?

  • @abdalrahmanaldammad8866
    @abdalrahmanaldammad8866 3 месяца назад

    I love you too

  • @Kaviarasu_S
    @Kaviarasu_S 3 месяца назад

  • @professorpoke
    @professorpoke 3 месяца назад

    He is using [a = 1, ..., z = 26], and still keeps saying that we need at least a base 26 system.
    Doesn't using [a = 1, ..., z = 26] make it a base 27 system already ??

    • @NeetCodeIO
      @NeetCodeIO  3 месяца назад

      good catch, i think you're right

    • @codewater4020
      @codewater4020 3 месяца назад

      @@NeetCodeIO correct me if i am not wrong base 2 means there are two base digits base 10 means there are 10 base digits not because the last digit is 9 as in base 16 we use 16 characters from 0 to 9 and then abcde etc so 0 id just a symbol i think for deciding base we only have to think about how many digits we are using, i might be wrong but that is what i think, so if i am wrong kindly help me.

  • @Kaviarasu_S
    @Kaviarasu_S 3 месяца назад

    A Simpler Approch with All Test Cases Passing:
    def shortestPalindrome(self, s: str) -> str:
    def is_palindrome(sub):
    return sub == sub[::-1]
    rev = s[::-1]
    for i in range(len(s)):
    if s.startswith(rev[i:]):
    return rev[:i] + s

    return ""

  • @ap2s2000
    @ap2s2000 3 месяца назад +1

    I love you too neet

  • @ethen8814
    @ethen8814 3 месяца назад

    沙发

  • @sivasaran_s
    @sivasaran_s 3 месяца назад

    class Solution:
    def shortestPalindrome(self, s: str) -> str:
    r = s[::-1]
    if not s: return ""
    for i in range(len(s)):
    if s.startswith(r[i:]):return r[:i] + s

  • @be_a_DT
    @be_a_DT 3 месяца назад +1

    Sorry , This Broke My steak Today 😢

  • @sauravsingh4497
    @sauravsingh4497 3 месяца назад

    love you too navdeep

  • @pabsy5801
    @pabsy5801 3 месяца назад

    the goat

  • @pixelbyteee
    @pixelbyteee 3 месяца назад

    You know it's crazy this can be done in just four lines...
    r = s[::-1]
    for i in range(len(s) + 1):
    if s.startswith(r[i:]):
    return r[:i] + s

    • @NeetCodeIO
      @NeetCodeIO  3 месяца назад +2

      isnt that an O(n^2) solution though

    • @pixelbyteee
      @pixelbyteee 3 месяца назад +1

      @@NeetCodeIO 😭 you're right

  • @dchennaraidu
    @dchennaraidu 3 месяца назад