Problem 272810 · medium · Phase 02 Linear Data Structures

One Deletion from a Palindrome

strings · palindromes · two pointers

Write almost_palindrome(s) that returns True if s is a palindrome, or can become one by deleting at most one character. Every character counts and comparison is case-sensitive.

Examples

Input:  s = "abca"
Output: True
Explanation: delete "c" (or "b").

Input:  s = "abc"
Output: False

Input:  s = "racecar"
Output: True

Constraints

  • 0 <= len(s) <= 10**5, printable ASCII
  • Target: O(n) time; trying every deletion is O(n^2) and too slow

Goals

  • Compare characters from both ends inward
  • Branch once at the first mismatch
  • Reuse a helper that checks a range for being a palindrome
Starting Python…