Palindromic Substrings - Leetcode 647 - Python

  Рет қаралды 159,122

NeetCode

NeetCode

Күн бұрын

Пікірлер: 101
@NeetCode
@NeetCode 2 жыл бұрын
🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤
@jonaskhanwald566
@jonaskhanwald566 3 жыл бұрын
My day is incomplete without listening this: "Hey everyone, welcome back! and let's write some more neat code today".
@algorithmo134
@algorithmo134 3 жыл бұрын
Amazing explanation! Im really curious what jobs did you have previously and where?
@lingyunsu1589
@lingyunsu1589 2 жыл бұрын
Truly: "Hey everyone, welcome back! and let's write some more neat code today".
@drstoneftw6084
@drstoneftw6084 2 жыл бұрын
Shorter solution that I found class Solution: def countSubstrings(self, s: str) -> int: res = 0 def pl(l,r) -> int: if l < 0 or r > len(s) - 1: return 0 if s[l] == s[r]: return 1 + pl(l - 1, r + 1) return 0 for i in range(len(s)): res += pl(i,i) + pl(i,i+1) return res
@seungjunlee00
@seungjunlee00 2 жыл бұрын
Currently completed around 40% of the BLIND list. Thanks neetcode for always very clear explaination!
@srinadhp
@srinadhp 3 жыл бұрын
I owe you one for making this concept so simple and crystal!
@richard12345789
@richard12345789 3 жыл бұрын
Bro dont stop posting videos, I will try to donate weekly for coffee and share link to atleast one person to support ur videos.
@NeetCode
@NeetCode 3 жыл бұрын
Thanks, appreciate that a lot 😃
@hwang1607
@hwang1607 Жыл бұрын
After watching longest palindromic substring neetcode video came up with this: class Solution: def countSubstrings(self, s: str) -> int: res = 0 def check(l,r): nonlocal res while l>= 0 and r < len(s) and s[l] == s[r]: res += 1 l -= 1 r += 1 for i in range(len(s)): check(i,i) check(i, i+1) return res
@americandream1989
@americandream1989 2 жыл бұрын
What a smart solution! I see this question is categorized as a DP problem on LC and was struggling of thinking to solve in a DP way. Really like the way you explain a question and find a much simpler way to solve it!
@電腦騙徒剋星
@電腦騙徒剋星 Жыл бұрын
the dp is O n^2 and even easier lmfao
@電腦騙徒剋星
@電腦騙徒剋星 Жыл бұрын
this can be solved in O n using weird string algo
@indraxios
@indraxios Жыл бұрын
loving blind 75 series, learning a lot
@nehascorpion
@nehascorpion 2 жыл бұрын
The best explanation on palindromic substrings so far! Thanks a ton!
@kirillzlobin7135
@kirillzlobin7135 Жыл бұрын
The clearest explanation all over the Internet
@IslamWaleed-gg4mc
@IslamWaleed-gg4mc 9 ай бұрын
14:06 what i did with 108 lines of code in cpp, he did in 16 lines of code in python
@zaki_1337
@zaki_1337 9 ай бұрын
I was able to solve this today by myself because i'd seen you do it a couple of months back. Thanks for everything man!
@dnm9931
@dnm9931 Ай бұрын
Keep making videos bro, you’re really good! thank you so much!
@enkr1
@enkr1 Жыл бұрын
You can also use Manacher's algorithm for O(n) and O(n) time space complexity!
@anusha3598
@anusha3598 Жыл бұрын
thank you so much neetcode. It is such a relief that when i am stuck i know i can rely on you to help me out and understand something
@mostinho7
@mostinho7 Жыл бұрын
Great explanation but not sure why this is categorized as dp on your website Same solution as longest palindrome problem (for each index expand outwards to find palindromes)
@johnnychang3456
@johnnychang3456 2 жыл бұрын
Beautiful and clean solution. Thank you. And I love your clean coding style!
@MP-ny3ep
@MP-ny3ep 9 ай бұрын
Terrific explanation. So well explained.
@alhdlakhfdqw
@alhdlakhfdqw 3 жыл бұрын
really great in depth explanation thank you so much! :) subbed
@NeetCode
@NeetCode 3 жыл бұрын
Happy it was helpful!
@rentianxiang92
@rentianxiang92 2 жыл бұрын
as always, thank you very much for your hard work, you just making me one step closer to my new career
@mdahadhossen6410
@mdahadhossen6410 10 ай бұрын
Thlanks for smooth explanation
@kautilyabhardwaj3400
@kautilyabhardwaj3400 4 ай бұрын
bestest teacher on youtube for leeeeeeeeeeeetcodeeeeeeeeee
@Iamnoone56
@Iamnoone56 3 жыл бұрын
Saw this expand from center technique in longest palindromic substring question soln tab on leetcode it was not so clear but now its super clear and i learned how to use the technique
@manishbolbanda9872
@manishbolbanda9872 5 ай бұрын
this was very much intuitive, thanks for the video
@CHUAN-CHI
@CHUAN-CHI 10 ай бұрын
Thank you!
@103himajapoluri6
@103himajapoluri6 2 жыл бұрын
Very great solution without usage of dp
@vincentlius1569
@vincentlius1569 2 жыл бұрын
Thanks man, my solution for this was Memory Limit Exceeded
@shoooozzzz
@shoooozzzz 2 жыл бұрын
Top tier resource as I study algos for upcoming Google interview
@NeetCode
@NeetCode 2 жыл бұрын
Thank you so much!
@ChocolateMilk116
@ChocolateMilk116 Жыл бұрын
thank you for explaining so clearly, this was really helpful
@karamany9870
@karamany9870 10 ай бұрын
Got this in an interview.
@sancho7198
@sancho7198 9 ай бұрын
google? :3
@karamany9870
@karamany9870 9 ай бұрын
Hackerrank OA for Goldman Sachs for new Grad.@@sancho7198
@DhritiChawla
@DhritiChawla 2 жыл бұрын
Woah mind blown. Thank you for this explanation!!
@sarthakjain1824
@sarthakjain1824 Жыл бұрын
Excellent explanation and code thank you so much
@utkarshsharma1185
@utkarshsharma1185 2 жыл бұрын
Thanks for phenomenal explanation 👏
@smartwork7098
@smartwork7098 5 ай бұрын
Beautiful solution.
@gillesyvetot8269
@gillesyvetot8269 2 жыл бұрын
This is so well explained! You da real MVP
@sweetpotatoeater3169
@sweetpotatoeater3169 2 жыл бұрын
This is absolutely beatuiful. Thank you sir!
@darrylbrian
@darrylbrian 2 жыл бұрын
Great video as per usual. I don't think it's necessary to pass in s into the countPalindromes helper function. It already has access to s and we're not changing anything to s when we pass it in.
@jesusvzla5199
@jesusvzla5199 2 ай бұрын
do you know Manacher’s algorithm? you can achieve O(n) time complexity on this problem
@RohanKumar-mh3pt
@RohanKumar-mh3pt Жыл бұрын
perfect explanation
@manojmpatil1269
@manojmpatil1269 3 жыл бұрын
Beautiful explanation. Thank you!
@CJ-ff1xq
@CJ-ff1xq 2 жыл бұрын
Quick question, why is this under the DP category? I don't see any DP in this solution?
@mucle6
@mucle6 Жыл бұрын
If you go from the outside in you need dp. a substring from range 0-100 is a palindrome if s[0] == s[100] and 1-99 is a palindrome. 1-99 is a palindrome if s[1] == s[99] and 2-98 is a palindrome. checking the middle is repeated work that we can use dp to get rid of So you could optimize the outside in approach to use a 2d dp array to get it down to n^2 Btw this wasn't obvious to me. It took me 10 minutes to figure out why going outside in was slower than inside out
@mohdalim77
@mohdalim77 2 жыл бұрын
If the applied approach is Two Pointers then why it is listed in Dynamic Programming? I am new following Neetcode 75 please exlplain
@kumarnishit6994
@kumarnishit6994 2 жыл бұрын
there are many ways to solve a problem. DP solution is equally beautiful, but it has a lot of unnecessary space complexity.
@rmiliming
@rmiliming 2 жыл бұрын
Thank you for your nice tutorial! Something seems a bit confusing to me and hopefully I can get some of your guidance! :) In the first while loop, would the res account for all single characters as palindromic substrings as well? Since l and r are both intialized as I and s[I]always = s[I] , which according to the line "res+=1", the res would be incremented by 1.
@muralisriram8035
@muralisriram8035 3 жыл бұрын
Great Explanation! Thank you
@namanvohra8262
@namanvohra8262 2 жыл бұрын
Thank you so much!
@ruslanklymenko1019
@ruslanklymenko1019 Жыл бұрын
Could someone explain why its under 'dynamic programming' ? In my opinion proposed solution doesn't use this technique. Am i wrong?
@dzianisdashkevich1848
@dzianisdashkevich1848 3 жыл бұрын
perfect explanation, thanks!
@ChiCity511
@ChiCity511 2 жыл бұрын
can we get a link to the spreadsheet with the descriptions please :)
@IK-xk7ex
@IK-xk7ex Жыл бұрын
I couldn't pass the test case #130 by this way, but all test cases from 1 to 129 passed :(
@jayrathod9271
@jayrathod9271 9 ай бұрын
Why the following approach is not working? class Solution: def countSubstrings(self, s: str) -> int: res = 0 n = len(s) #odd for i in range(n): l = i r = i while(l>=0 and r=0 and r
@barnetthan9391
@barnetthan9391 9 ай бұрын
instead of having if s[i]==s[r] be an if statement inside the while loop, make it another condition for the while loop to run: while (l>=0 and r
@twhizzay
@twhizzay 2 жыл бұрын
In the brute force method, why is it n^2 substrings? In the 'aaab' example, I am only counting 10 substrings, not 16 (i.e. n^2 = 4^2 = 16).
@johnnychang3456
@johnnychang3456 2 жыл бұрын
If you put it in code it would look like this: for i in range(len(s)): for j in range(i, len(s): subStr = s[i:j+1] print(subStr) You're right that it does not execute n^2 times, but I think in big O notation it is O(n^2).
@kwaminaessuahmensah8920
@kwaminaessuahmensah8920 2 жыл бұрын
you’re a god
@amitupadhyay6511
@amitupadhyay6511 2 жыл бұрын
is there any way we can use recursion with memoization for this question ?
@Kumar08
@Kumar08 Жыл бұрын
in "abc" why you didnt considered ab and bc as palindrome?
@rohanvishayal8724
@rohanvishayal8724 5 ай бұрын
Because ab != ba , same as bc != cb
@lubdhakmahapatra8476
@lubdhakmahapatra8476 2 жыл бұрын
@NeetCode: Can i have the Eth column of the google sheet in the starting of the video ?
@jorgegonzaloalfaro5378
@jorgegonzaloalfaro5378 3 жыл бұрын
thank you for this!!!
@PanashePhotography
@PanashePhotography 3 жыл бұрын
great video
@rathhen8384
@rathhen8384 3 жыл бұрын
amazing video
@papirreo
@papirreo Жыл бұрын
I would tell the interviewer DRY code is not the end all be all. And then dry it.
@marksaunders8430
@marksaunders8430 3 жыл бұрын
Hey man, thanks for the video. Do you mind posting your code in future videos? I probably miss something small and then the solution doesn't work so I spend a long time debugging it. Will appreciate it!
@bhailog_gaming_2000
@bhailog_gaming_2000 3 жыл бұрын
Excellent
@ameynaik2743
@ameynaik2743 3 жыл бұрын
Which tool do you use to draw out?
@NeetCode
@NeetCode 3 жыл бұрын
I use Paint3D
@braindeadcow7346
@braindeadcow7346 Жыл бұрын
Are you for real? My interviewer gave me 2 hints for 2d dp tf?
@akshitmiglani5419
@akshitmiglani5419 2 жыл бұрын
How will this solution catch abba? I'm just curious.
@blackwolf6687
@blackwolf6687 Жыл бұрын
I see that the even Palindromes will catch that. starting at the first 'b', l=b and r=b
@mahf7739
@mahf7739 Жыл бұрын
awesome
@vigneshsvsv
@vigneshsvsv 3 жыл бұрын
Thanks for the solution. Is there any process that I can follow to think of a solution like this on my own?
@sandypatel_
@sandypatel_ 9 ай бұрын
why this qn in 1D dp?
@rahatsshowcase8614
@rahatsshowcase8614 2 жыл бұрын
nice
@raylei4700
@raylei4700 2 жыл бұрын
Hi, I tried to use similar code to solve leetcode 516 longest palindrome subsequence(not substring). But got incorrect answers. Can you please explain how to solve it? my code is as follows max_len = 0 for i in range(len(s)): # odd case l, r = i, i cur_max = 0 while l >= 0: while r < len(s): if s[l] == s[r]: cur_max = cur_max + 1 if l == r else cur_max + 2 r += 1 break else: r += 1 l -= 1 max_len = max(max_len, cur_max) # similar for the even case
@ShikshaMishraMTCS
@ShikshaMishraMTCS 2 жыл бұрын
so are you still in google??
@chehakmalhotra7807
@chehakmalhotra7807 Жыл бұрын
whys this DP
@marksaunders8430
@marksaunders8430 3 жыл бұрын
Fails the test case "abccccdd"
@marksaunders8430
@marksaunders8430 3 жыл бұрын
output is 15, expected is 7
@rathhen8384
@rathhen8384 3 жыл бұрын
@@marksaunders8430 single characters are palindrome so a,b,c,c,c,c,d,d should be 8 palindromes. Then cccc -> is 4 palindromes. Then dd which gives 15 total
@adamsloma3850
@adamsloma3850 2 жыл бұрын
1000th like
@richard12345789
@richard12345789 3 жыл бұрын
hi bro,Why dont u do twitch streaming+coding once a month
@NeetCode
@NeetCode 3 жыл бұрын
Yeah, I do have plans for different kinds of videos and also live streaming, Q/A, etc. For the next month I'm gonna be pretty busy with life stuff, but after that I'm definitely gonna do some streaming
@siddharthaarora4351
@siddharthaarora4351 2 ай бұрын
wow?
@naveenprasanthsa3749
@naveenprasanthsa3749 3 жыл бұрын
Hey u have already done this problem in Longest palindromic substring. Do more problems on data structures, graphs, job schedulings.
@jorgegonzaloalfaro5378
@jorgegonzaloalfaro5378 3 жыл бұрын
where are your manners smh.
@omkarjadhav848
@omkarjadhav848 2 жыл бұрын
Thank you for the great explanation.
@rryann088
@rryann088 Жыл бұрын
beautifully explained! Thank you !!!
Longest Repeating Character Replacement - Leetcode 424 - Python
20:44
Climbing Stairs - Dynamic Programming - Leetcode 70 - Python
18:08
Creative Justice at the Checkout: Bananas and Eggs Showdown #shorts
00:18
Fabiosa Best Lifehacks
Рет қаралды 8 МЛН
УДИВИЛ ВСЕХ СВОИМ УХОДОМ!😳 #shorts
00:49
HARD_MMA
Рет қаралды 3,3 МЛН
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 60 МЛН
Longest Palindromic Substring Manacher's Algorithm
16:46
Tushar Roy - Coding Made Simple
Рет қаралды 343 М.
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 253 М.
L17. Palindrome Partitioning | Leetcode | Recursion | C++ | Java
24:34
take U forward
Рет қаралды 286 М.
Longest Palindromic Substring - Python - Leetcode 5
8:11
NeetCode
Рет қаралды 557 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 695 М.
Coding Interviews Be Like
5:31
Nicholas T.
Рет қаралды 6 МЛН
LeetCode 5.  Longest Palindromic Substring (Algorithm Explained)
14:40
Creative Justice at the Checkout: Bananas and Eggs Showdown #shorts
00:18
Fabiosa Best Lifehacks
Рет қаралды 8 МЛН