Google Engineer Explains - LeetCode #5 - Longest Palindromic Substring - Solution (Python)

preview_player
Показать описание
Explanation of LeetCode #5 - Longest Palindromic Substring with Python Solution and Time and Space Complexity Analysis and Discussion of which approach to use for an interview.
(Medium Difficulty)

01:17 Which Approach to Use
04:01 Interviewing Background
07:48 Overview of Approach
13:32 Python Implementation
26:34 Time and Space Complexity
Рекомендации по теме
Комментарии
Автор

I loved how you approach the possible solutions and give tips for interviews, not jump to solution right away. Thanks!

ahmetlekesiz
Автор

I really appreciate your videos! Very clear and to the point. Definitely looking forward to more content. Cheers.

kaushikbhimraj
Автор

Thank you! It helped me to understand :) Go Bears! 2017 alumni here.

ksjksjwin
Автор

Finally someone explains the weird indexing in this problem

koownmyo
Автор

hi, great explanation, thanks for sharing this video.

lxkp
Автор

Much appreciate the video, I do have some questions though:
1. Is it more efficient to have 3 loops without passing the substring or 2 loops, but passing the substring? By passing the string, I mean instead of invoking and comparing one character at a time, to recover the whole substring between left and right index and check it against it's reversed version. Example: s[l:r+1] == s[l:r+1][::-1]?
2. Is recovering the substring s[l:r+1] inside a while or an if statement just as inefficient as recovering it inside the return statement? Example: while s[l:r+1] == s[l:r+1][::-1]:... vs return s[l:r+1]
3. In a while loop, if there are two conditions, does the loop stop as soon as the earlier (left-most) condition is not satisified? Example: Is while l >= 0 and r < len(s) and s[l] == s[r]: and while s[l] == s[r] and l >= 0 and r < len(s) : equivalent to each other? Is the first option better, since it would break the loop at the edges of the string, not bothering to do string comparisons? This part was not very clear.
4. Is a single letter a palindrome? Example: 'b'
5. It seems to me we need only to track either (length, left) or (length, right). Both combinations will give the position of the opposite index, so there is no need to track one more variable.

slavrine
Автор

skip to 7:49 to start the actual algo. Watch in 1.5x

anumsheraz
Автор

I came up with this solution with few lines of code and it got accepted
```
class Solution:
def longestPalindrome(self, s: str) -> str:
def helper(l, r):
while(l>=0 and r< len(s) and s[l] == s[r]):
l -= 1
r += 1
return s[l+1:r]
res = ""
for i in range(len(s)):
test = helper(i, i)
if len(test) > len(res): res = test
test = helper(i, i+1)
if len(test) > len(res): res = test
return res
```

abdoulayebalde
Автор

Hey actually it was a very good video but i have one doubt.... At the end why did u return right + 1 ??? Instead of right

algominds
Автор

Hey Kevin. This is Tuhin from India. I was hoping this video was about Manacher's algorithm. Anyway, I would really appreciate it if you leave your ig id(if you have one) or maybe make a video about how one could land a job at Google. Thanks.

tuhinmukherjee
Автор

I think critical comments help in improving content. So... Too much introduction, you said that the complicated algorithm is not necessary a lot of times, too many times. I for one got bored and now I am looking for another video explaining it.

TheAlexeey
welcome to shbcf.ru