https://leetcode.com/problems/valid-palindrome-ii/
Intuition:
- Set two iterators from the beginning and the end and make adjustments
Gotchas:
- the deletion might be needed from the either side of the iteration. So from the point of the mismatch, two verifications are needed
- slicing the char character may be tricky
Base cases:
- None
Solution:
class Solution:
def validPalindrome(self, s: str) -> bool:
i = 0
j = len(s) - 1
used = False
while i <= j:
if not s[i] == s[j]:
return s[i:j] == s[i:j][::-1] or s[i+1:j+1] == s[i+1:j+1][::-1] # indexing
i += 1
j -= 1
return True
Complexity Analysis:
- O(N)
Comments
comments powered by Disqus