Given a string s, return true if the s can be palindrome after deleting at most one character from it.
Example 1: Input: s = “aba” Output: true Example 2: Input: s = “abca” Output: true Explanation: You could delete the character ‘c’.
- code
class Solution:
def validPalindrome(self, s: str) -> bool:
def helper(s):
return s == s[::-1]
l, r = 0, len(s) - 1
while l < r:
if s[l] == s[r]:
l += 1
r -= 1
else:
if s[l] == s[r-1]:
r -= 1
if helper(s[l:r+1]):
return True
r += 1
if s[l+1] == s[r]:
l += 1
if helper(s[l:r+1]):
return True
return False
return True
- code
class Solution:
def validPalindrome(self, s: str) -> bool:
l, r = 0, len(s) - 1
while l < r:
if s[l] != s[r]:
mov1 = s[l+1:r+1]
mov2 = s[l:r]
if mov1 == mov1[::-1] or mov2 == mov2[::-1]:
return True
return False
l += 1
r -= 1
return True