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!
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
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
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
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
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
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.
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
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
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
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
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 ?
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.
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 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]
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 :(
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; } };
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 ?
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 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.
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
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
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!
8:44 - Correction, the prefix of length 3 doesn't match suffix of length 3, i mispoke
Everyone makes mistakes
Bro, overr the years your teaching style has improved a lot. Can you re-record the important Algo like KMP.
Bruh you're the reason I'm solving daily challenges these days
I hope we can finish September LeetCoding challenge with this channel.
Intro put a smile on my face. Haven't heard that in a while because of the interview grind. Love you too Navdeep 🤝🤝
love you too, neet!
Honestly , Leetcode should make you their editor-in-chief
We enjoy these videos too! Hope you never stop making them! Thank you very much!
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
love you too navi! thanks a ton buddy
I enjoy the random reaction videos you do because you provide intuitive insight. You should continue doing them
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
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
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
we love you bro these videos are insane quality even if they get less views please keep at em 🙏🏽
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.
I am really glad that you enjoy making these, because I have learned a shitload from you.
stuck in the test case and waiting for u :))) i love neetcode
❤ Navdeep Singh
casual way of presenting hard problems
so sad these lc videos don't get nearly as many views 😞
We love you too ❤🧡💛💚💙💜🤎🖤🤍
theres no way you could come up with the second solution on your own without help unless your a genius
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
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.
I still don't get it. Why prefix * base but for suffix its char * power 😭. Wasted whole day still couldn't get it.
Thanks for explaining the rolling hash . Before watching this video i struggled a lot to understand the concept. :)
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.
Thanks for everything, teacher.
This is a brilliant explanation. Glad i watched it.
Thank you Neetcode. Love you too :)
Isn't what you're describing at 13:38 left shift and not right shift?
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
Love you too Neet!
You r awesome,Thank You
i spent like 2 hours trying to tweak manachers algorithm wish i had watched this before
Great insights into problems, thank you.
Are you always able to come up with a solution on your own?
Thank you for making the video, please make video for every daily problems everyday
Such a nice solution.
❤you and your videos Neet !
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
Love you too man ...
the famous rolling hashes🥱🍃
love you too, neet.
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
Thank you for making these videos everydat
Great explanation
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
thanks as usual. Any intuition behind using base 29, you definitely mentioned at least 26, but wondering if there's anything that went behind?
we love you tooo!!
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 ?
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
Love you too bro
We love you too
WE LOVE YOU TOO
noob question, if hashing/
encoding iss the way, cant we use a hashmap directly? keys are always hashed?
much love mr neet
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.
GOOD EXPLANATION
bro can you make a video on morris traversal
Nice one
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
doing s[::-1] creates new copy.. so for sure two pointer is better
@@_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]
Luv u 2 man
8:47 how does "aac" match "caa"?
They match when you read aac left to right and caa right to left.
I love u neetcode
can we use a stack?
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 :(
love you too :)
i'v lost all the motivation after gpt o1
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;
}
};
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 ?
for axxxb lps is just "a" and not xxx
I have tried to understand KMP three times but failed.
i dont understand why 29
0:06 Huh, is the problem that hard
Or you up to something ?
I love you too
❤
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 ??
good catch, i think you're right
@@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.
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 ""
easier to write, but still O(n^2)
I love you too neet
沙发
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
Sorry , This Broke My steak Today 😢
love you too navdeep
the goat
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
isnt that an O(n^2) solution though
@@NeetCodeIO 😭 you're right