Valid Palindrome II - Leetcode 680 - Python

  Рет қаралды 49,227

NeetCode

NeetCode

Күн бұрын

Пікірлер: 43
@chagsssss
@chagsssss 2 жыл бұрын
Great flow of explanation. I try to solve a problem and regardless of whether I can or not, I watch the video of the problem on your channel. Either way, it helps build up the fundamentals for me.
@gianniprocida3332
@gianniprocida3332 2 жыл бұрын
Yes definitely
@CJ-ff1xq
@CJ-ff1xq 2 жыл бұрын
Reversing a string is not really the optimal way to determine if it’s a palindrome. It uses extra memory and you also have to iterate over the whole string. With two pointers you only iterate half the string. Still O(n) but should be a bit faster on average! But I see you mentioned it at the end of the video anyway so great job.
@numberonep5404
@numberonep5404 2 жыл бұрын
a solution with pointers, trivial but still class Solution: def validPalindrome(self, s: str) -> bool: def isPal(lf, rh): while lf
@harigovind11
@harigovind11 2 жыл бұрын
One of Facebook's favourite interview question. Was asked this twice by Facebook itself.
@swarupsarangi734
@swarupsarangi734 2 жыл бұрын
# USING THE HELPER FUNCTION class Solution: def helper(self,s,p1,p2) : while p1 < p2 : if s[p1] != s[p2]: return False p1 += 1 p2 -= 1 return True def validPalindrome(self, s: str) -> bool: l, r = 0, len(s) - 1 while l < r: if s[l] != s[r]: # check if elements l + 1 till r + 1 is pallindrome # check if element l till r is pallindrome return ( self.helper(s,l + 1, r) or self.helper(s,l, r - 1) ) l += 1 r -= 1 return True
@Ankit-hs9nb
@Ankit-hs9nb 2 жыл бұрын
with helper function O(1) space class Solution: def validPalindrome(self, s: str) -> bool: left = 0 right = len(s) - 1 while left < right: if s[left] != s[right]: return self.is_palindrome(s, left+1, right) or self.is_palindrome(s, left, right-1) left += 1 right -= 1 return True def is_palindrome(self,s, start_index, end_index): while (start_index < end_index): if s[start_index] != s[end_index]: return False start_index += 1 end_index -= 1 return True
@nilanjandey9604
@nilanjandey9604 Ай бұрын
ohk. Now i understood. i have to skip and check that whether the remaining string is palidrome or not. got it. Thanks
@aghilannathan8169
@aghilannathan8169 2 жыл бұрын
Absolutely no fucking way. I was doing Valid Palindrome and for whatever reason, I just could not solve it. I open KZbin to see if NeetCode has a video on it and you posted it 30 minutes ago, just my luck.
@riyaz3409
@riyaz3409 Жыл бұрын
class Solution: def validPalindrome(self, s: str) -> bool: def checkValid(l,r): while l
@dice4572
@dice4572 11 ай бұрын
I tried doing this by skipping the element that's not equal (if s[l+1] == s[r]: l+=1 and vice versa) and i almost understand why that doesn't work but not quite. can someone pls explain why this strat doesn't work?
@aryehakierman6388
@aryehakierman6388 3 ай бұрын
I also couldnt understand why it didnt work untill I found this use case: "ececabbacec"
@flatmapper
@flatmapper Жыл бұрын
Thanks NeetCode. I love my solution without allocating extra memory fun validPalindrome(string: String): Boolean { var i = 0 var j = string.lastIndex while (i < j && string[i] == string[j]) { i++ j-- } return isPalindrome(string, i + 1, j) || isPalindrome(string, i, j - 1) } fun isPalindrome(string: String, start: Int, end: Int): Boolean { var i = start var j = end while (i < j) { if (string[i] != string[j]) { return false } i++ j-- } return true }
@jbaltariu
@jbaltariu Жыл бұрын
I still don't get it. Why isn't it O(n^2) if you're reversing the string each time you check for a palindrome? Isn't that operation linear as well?
@aryehakierman6388
@aryehakierman6388 3 ай бұрын
good question,the reason its not O(N^2) is because we arnt reversing the string for every character in the string,we are at most reversing them once or twice, therefor it is O(N+2N) => O(N)
@geekydanish5990
@geekydanish5990 2 жыл бұрын
class Solution: def validPalindrome(self, s: str) -> bool: def is_valid_palindrome(l,r): while l < r: if s[l] != s[r]: return False l +=1 r -=1 return True l_pointer = 0 r_pointer = len(s)-1 while l_pointer < r_pointer: if s[l_pointer] == s[r_pointer]: l_pointer +=1 r_pointer -=1 else: if is_valid_palindrome(l_pointer+1,r_pointer): return True if is_valid_palindrome(l_pointer,r_pointer-1): return True return False return True
@barissimsek
@barissimsek 9 ай бұрын
It's not clear from the question but this solution won't work if there are two differences in the string as it returns on the first occurrence. The questions says at most one change but it doesn't say there is only one diff max.
@harishkumarbikki1060
@harishkumarbikki1060 Жыл бұрын
In skipL, keeping r+1 as higher limit and will it cause index out of bound error?
@rajatagrawal7016
@rajatagrawal7016 2 жыл бұрын
Java class Solution { public boolean validPalindrome(String s) { char ar[]= s.toCharArray(); int j=ar.length-1; int i=0; for(;i
@inessben8679
@inessben8679 2 жыл бұрын
Hello neetcode, Thank you so much for your amazing and clear videos. I have a request, Can you please explain a problem called " Minimum steps to reduce to 1 " ?
@tanayraj2991
@tanayraj2991 2 жыл бұрын
Is it possible to do this using LCS? If the LCS of the string and its reverse version is less than str.length-1 then it is not valid palindrome.
@apiyush
@apiyush 9 ай бұрын
Shouldn't the time complexity be O(n^2) since we are iterating over n//2 times and using string reversal (i.e. s == s[::-1]) ? (n//2) * (n) ~= O(n^2) ?
@quickscope754
@quickscope754 5 ай бұрын
You would only need to use the string reversal at most twice though because once you have an s[l] that does not match and s[r], you return either true or false and do not continue checking the rest of the string. Therefore its just O(n).
@madhumalar789
@madhumalar789 2 жыл бұрын
Can I get guidelines to practice on ABAP in the site you mentioned ?
@datboi20
@datboi20 2 жыл бұрын
I'm a little confused about how this solution is not runtime of O(n^2). The outside while loop is 0(n). Then the reversal (skipL/skipR[::-1]) inside of the while loop is O(n). Since it's nested, I'd think that would be O(n^2). Am I missing something?
@rjesh2062
@rjesh2062 Жыл бұрын
No you are travelling each character at most one time which is n steps which means it should be O(length) at worst case
@randomlyasked
@randomlyasked 2 жыл бұрын
Any chance of posting leetcode contest solutions? 3rd and 4th if possible 🥺
@vdyb745
@vdyb745 2 жыл бұрын
Awesome !!!! I admire your commitment !!! Thank you so much ....
@thanirmalainagappan9472
@thanirmalainagappan9472 2 жыл бұрын
Thank you very much
@tranminhquang4541
@tranminhquang4541 5 ай бұрын
Why th is this one labeled "easy"?
@ammarhindi9253
@ammarhindi9253 2 жыл бұрын
Can you create a hash map and check if the number of Keys in the map is greater than 2 return false since there is more than two unique characters that if we choose to delete any of them it would still not be a palindrom. Return true if the amount of Keys is 2 or less?
@rjesh2062
@rjesh2062 Жыл бұрын
For anyone having the same doubt it would not work coz you cannot change the positions of the characters and count of characters will not give you a Idea about whether a string is a palindrome or Not..Eg: acdacc , aadccc
@rajatagrawal7016
@rajatagrawal7016 2 жыл бұрын
Keep going I wait for your videos.
@AntoineJacques
@AntoineJacques 2 жыл бұрын
def validPalindrome(self, s: str) -> bool: l=0 r=len(s)-1 while l < r: if s[l] == s[r]: l+=1 r-=1 else: return s[l:r] == s[l:r][::-1] or s[l+1:r+1] == s[l+1:r+1][::-1] return True
@_Ahmed_15
@_Ahmed_15 Ай бұрын
Should be a medium
@__________________________6910
@__________________________6910 2 жыл бұрын
Again I'm first NeetCode
@coder4937
@coder4937 2 жыл бұрын
Congrats🎉
@NeetCode
@NeetCode 2 жыл бұрын
lol nice streak
@masternobody1896
@masternobody1896 2 жыл бұрын
@@NeetCode reverse link list
@sproutboot
@sproutboot 2 жыл бұрын
2:00
@alanhuang7831
@alanhuang7831 2 жыл бұрын
Heyy
VALID PALINDROME III | LEETCODE 1216 | PYTHON MEMOIZED DFS SOLUTION
16:01
How I Failed the Google Coding Interview (and lessons I learned)
14:24
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН
Amazing remote control#devil  #lilith #funny #shorts
00:30
Devil Lilith
Рет қаралды 16 МЛН
ТЮРЕМЩИК В БОКСЕ! #shorts
00:58
HARD_MMA
Рет қаралды 2,6 МЛН
What type of pedestrian are you?😄 #tiktok #elsarca
00:28
Elsa Arca
Рет қаралды 32 МЛН
Maximum Frequency Stack - Leetcode 895 - Python
13:21
NeetCode
Рет қаралды 28 М.
Take K of Each Character From Left and Right - Leetcode - Python
17:19
Valid Palindrome - Leetcode 125 - Python
14:58
NeetCode
Рет қаралды 278 М.
Word Break II - Leetcode 140 - Python
20:39
NeetCodeIO
Рет қаралды 15 М.
VALID PALINDROME II [PYTHON]
11:45
Cracking FAANG
Рет қаралды 1,5 М.
Sentence Similarity III - Leetcode 1813 - Python
15:07
NeetCodeIO
Рет қаралды 10 М.
Simplify Path - Stack - Leetcode 71 - Python
12:47
NeetCode
Рет қаралды 62 М.
Baseball Game - Leetcode 682 - Python
7:02
NeetCode
Рет қаралды 36 М.
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН