Valid Palindrome with At Most One Removal
StringsAuto-graded · 23 test casesLast updated
Determine whether a given string can be converted into a palindrome by removing no more than one character.
Given a string s containing lowercase letters, determine if it is possible to transform s into a palindrome by removing at most one character from any position.
A string is a palindrome if it reads identically from left to right and right to left. Removing zero characters is permitted, meaning any string that is already a palindrome satisfies the condition.
Constraints
1 <= len(s) <= 10^5sconsists exclusively of lowercase English characters (a-z).
Examples
Example 1
Input: can_be_palindrome_after_one_deletion('abca')
Output: True
Example 2
Input: can_be_palindrome_after_one_deletion('abcbaa')
Output: True
Example 3
Input: can_be_palindrome_after_one_deletion('abc')
Output: False
Hints
Hint 1
Use two pointers starting from opposite ends of the string and move inward as long as the characters match.
Hint 2
When you encounter the first mismatch at indices `left` and `right`, you must remove either `s[left]` or `s[right]` to proceed.
Hint 3
Check if the remaining substring—either `s[left+1 .. right]` or `s[left .. right-1]`—is a strict palindrome without any further removals.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
We can solve this efficiently using a two-pointer approach:
- Place one pointer
leftat index0and another pointerrightat indexlen(s) - 1. - Move both pointers inward as long as
s[left] == s[right]. - If
leftandrightmeet without finding any character mismatch, the string is already a palindrome, so we returnTrue. - If a mismatch occurs at indices
leftandright(i.e.,s[left] != s[right]), we must consume our single allowed removal. To resolve the mismatch, we must discard either the left characters[left]or the right characters[right].- If discarding
s[left]makes the remaining slices[left+1 .. right]a valid palindrome, the overall answer isTrue. - If discarding
s[right]makes the remaining slices[left .. right-1]a valid palindrome, the overall answer isTrue. - If neither sub-range is a strict palindrome, it is impossible to form a palindrome with only one deletion, so we return
False.
- If discarding
Complexity Analysis
- Time Complexity: $\mathcal{O}(n)$, where $n$ is the length of
s. We scan the string inward until the first mismatch, and then execute at most two linear verification checks on the remaining sub-segment. - Space Complexity: $\mathcal{O}(1)$ auxiliary space if index comparisons are performed without allocating string slices.
Python Solution
def can_be_palindrome_after_one_deletion(s: str) -> bool:
def is_palindrome_range(left: int, right: int) -> bool:
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
# Spend the single deletion on either the left or right character
return is_palindrome_range(left + 1, right) or is_palindrome_range(left, right - 1)
left += 1
right -= 1
return True