Skip to content

Palindrome After One Deletion

Easy#two-pointers#greedy~20 min

The mission

Problem

A word game accepts a string if it reads the same forwards and backwards, and it forgives a single typo. Given a string s of lowercase letters, return True if s is already a palindrome or becomes one after deleting exactly one character, and False otherwise. The empty string counts as a palindrome.

Examples

Input:  s = "racexcar"
Output: True
Why:    deleting the x leaves "racecar"
Input:  s = "abcda"
Output: False
Why:    after the outer a's match, b and d disagree, and dropping either one still fails
Input:  s = ""
Output: True
Why:    edge case, nothing to compare, so no deletion is needed

Hints

0 / 3

Target time
O(n)
Target space
O(1)

Constraints

  • s holds lowercase letters
  • the empty string counts as a palindrome

Stuck on the idea rather than the code? Valid Palindrome covers it.

Python 3Ctrl+Enter runs
Custom test case