40. Valid Palindrome II
Problem Statement
Given a string s
, return true
if the s
can be a 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'.
Example 3:
Input: s = "abc" Output: false
Solution
class Solution:
def validPalindrome(self, s: str) -> bool:
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
# Mismatch found. Try deleting s[left] or s[right]
# Check if s[left+1...right] is a palindrome
# Check if s[left...right-1] is a palindrome
return self.is_palindrome_range(s, left + 1, right) or \
self.is_palindrome_range(s, left, right - 1)
left += 1
right -= 1
return True
def is_palindrome_range(self, s: str, left: int, right: int) -> bool:
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
Explanation
This problem extends the basic palindrome check. We are allowed to delete at most one character.
The approach uses a two-pointer technique.
-
Initial Two-Pointer Scan: We use two pointers,
left
starting at the beginning andright
starting at the end of the strings
. -
Mismatch Handling: We iterate inward as long as
s[left]
equalss[right]
.- If
s[left]
ands[right]
are not equal, it means we've found a character that needs to be potentially deleted. At this point, we have two choices:- Delete
s[left]
: Check if the substrings[left+1 ... right]
is a palindrome. - Delete
s[right]
: Check if the substrings[left ... right-1]
is a palindrome.
- Delete
- If either of these two resulting substrings is a palindrome, then the original string
s
can be made a palindrome by deleting at most one character, so we returnTrue
.
- If
-
is_palindrome_range
Helper: A helper functionis_palindrome_range(s, left, right)
is used to check if a given substring (defined byleft
andright
indices) is a palindrome. -
No Mismatch: If the
while
loop completes without finding any mismatches, it means the original strings
is already a palindrome, so we returnTrue
.
The time complexity is O(n) because in the worst case, we traverse the string once with the main loop, and then potentially twice more with the is_palindrome_range
helper, each taking O(n) time. The space complexity is O(1).