We are asked to determine if a string s can become a palindrome after deleting at most one character.
Constraints:
1 <= s.length <= 10^5sconsists of lowercase English letters.
Key insight:
- A normal palindrome check compares characters from both ends inward.
- If we find a mismatch, we have one chance to delete either the left character or the right character, then continue checking.
- If either option yields a palindrome, return
true; otherwise returnfalse.
-
Use two pointers:
left = 0right = s.size() - 1
-
While
left <= right:- If
s[left] == s[right], move inward:left++,right--.
- Otherwise (first mismatch):
- Try skipping
s[left]→ check ifs[left+1..right]is palindrome. - Try skipping
s[right]→ check ifs[left..right-1]is palindrome. - If either is palindrome, return
true.
- Try skipping
- If
-
If loop finishes with no mismatch or after one valid skip, return
true.
- Two-pointer + one deletion: Only need to check at most two substrings after first mismatch.
- Helper function:
isPalindrome(s, left, right)efficiently checks a substring. - Early stop: We don’t need to test all deletions, just the first mismatch case.
- Time: O(n)
- Each character is checked at most twice: once in main loop, once in
isPalindrome.
- Each character is checked at most twice: once in main loop, once in
- Space: O(1)
- Only pointers and counters used, no extra data structures.