Palindrome After Deleting At Most One Character

Implement canBeAPalindromeAfterOneRemoval

Given a string s, return true if it can become a palindrome by deleting at most one character. Walking two pointers inward from both ends handles the matching characters for free. The only real decision point is the first mismatch: since only one deletion is allowed, there are exactly two ways to fix it — drop the character at the left pointer, or drop the one at the right pointer. Whichever choice leaves a palindrome in the remaining range makes the answer true. Checking both candidate ranges still keeps the whole algorithm at O(n), since every character is visited only a small, constant number of times.

Example 1:

Input: s = "abbca"

Output: true

Example 2:

Input: s = "abc"

Output: false

Example 3:

Input: s = "aba"

Output: true

+ 8 hidden test cases run on Submit.

Constraints:

  • 1 ≤ s.length ≤ 10⁵
  • s consists of lowercase English letters only

s =

abbca