Skip to content
BytePatterns

Palindrome After One Deletion

EasyStrings#two-pointers#greedy~20m

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

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