Valid Palindrome - Leetcode 125 - Python

preview_player
Показать описание


0:00 - Read the problem
1:40 - Explaining Solution #1
3:45 - Coding Solution #1
5:40 - Explaining Solution #2
9:35 - Coding Solution #2

leetcode 125

#coding #interview #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

It's so damn satisfying having done a solution and seeing that Neetcode uses the absolute same method.

davidfranke
Автор

I'm currently prepping to interview with Google in a couple months. Just wanted to let you know you've been an extremely helpful resource in getting ready for this! Thank you so much and hoping all is well :)

callmebiz
Автор

Yes!!! Thank you for continuing with the LC 75 series!! We all really appreciate it.

johns
Автор

The reason why the first solution is faster than the second one is that the method isalnum() is written in C behind the scenes while the solution you achieved is written in pure Python. C is a much more performant language than python, so thats why. Interestingly, a large part of Python methods are written in C.

HowToAiNow
Автор

Actually, we can write function for checking alphanumeric characters like this and it will work
def isalnum(c: str) -> bool:
return (("a" <= c.lower() <= "z") or ("0" <= c <= "9"))
We don't need to use ord()

Extremesarova
Автор

I picked up blind-75 aswell! Keep up the grind as always 💪

wasbashing
Автор

Heres an optimization ive noticed....you can avoid having to write while l < r in the checking parts again by changing it to an if statement and using continue.. so it would be something like:
if not alphanum(s[l]) :
l+=1
continue
same for right pointer
if not alphanum(s[r]) :
r-=1
continue

turtlenekk
Автор

Could someone elaborate on why he includes "l < r" and "r > l" in the internal while loops? My assumption is that the outermost while loop already accounts for that piece of logic (while l < r:...)

youngmoneymahini
Автор

I'd let out a big sigh if the interviewer asked for another approach after I show him the first solution

nero
Автор

The nested while loops is a bit hard to read. We could easily do with if else statements:
while l < r:
if not self.alphaNum(s[l]):
l += 1
elif not self.alphaNum(s[r]):
r -= 1
elif s[l].lower() == s[r].lower():
l += 1
r -= 1
else:
return False
return True

adventurer
Автор

7:03 Yeah, but ord() and chr() are also built-in functions like isallnum().

ah-rdk
Автор

I'm really glad that you use a different solution here that is actually O(n) rather than the advanced algorithms course. I see in the advanced algorithms course that you clean the string using appending method to a new string. In fact, if this is a paid course it is IMPERATIVE that you clarify that the method used there is polynomial time to clean the string. Because under the hood you are doing 1 + 2 + 3 ... N operations since it instantiates a new string each time you append a char. That's the impression I'm under, someone correct me if i'm wrong, and if that's not clarified in the course then it's really going to be a shame when someone fails an interview because of it.

AustinCS
Автор

The first solution is actually O(n^2) time where n is number of alphanumeric characters.
This is because python strings are immutable. "Appending" a character creates a new string each iteration.
It's better to build the new string with a list, and then using join later.

DonJulio-hruj
Автор

i ended up using this is_alnum function instead of the one used in the solution. it made more sense to me than using the <= approach:

Def is_alnum(self, c):
return ord(c) in range(48, 58) or ord(c) in range(65, 91) or ord(c) in range(97, 123)

the ranges correspond to the ascii vales for digits 0-9, uppercase letters, and lowercase letters respectively.

SteeleJackson
Автор

Helpful video. One can also perform a regex check to make sure the character values are in the alphanumeric range

Xushkkkk
Автор

Thank you for making these resources available

ShadyAlwidyan
Автор

After LC 75 is done, can you do the SeanPrashad 170 questions? If you have both playlists, that would be HUGE

johns
Автор

I solved this already, but coming here I see instead of remembering ASCII code it is fine to use the function and this is was a saviour.

kapilpatel
Автор

Ok, I'm going to be positive and say that, this is a great question. What it thought me stayed and made me a better programmer.

m_jdm
Автор

Took me minutes to a variant of the first solution (using comprehension list), whereas other medium challenges in LeetCode take me hours and I often need to check the solution to get it, the difficulty level is all over the place on LeetCode, I don't get how they rank it.

crikxouba
visit shbcf.ru