Skip to content

visual walkthrough

Valid Palindrome II

EasyOpposite EndsReported at: MetaAmazonApple+4

Allows one deletion, so you learn to branch once and re-check; a Meta favourite.

Solve on LeetCode

The idea

Walk inward from both ends exactly as in the plain palindrome check. Only at the first mismatch do you need a decision: the extra letter is either the left one or the right one.

Check whether the remaining middle is a palindrome with the left letter skipped, and again with the right one skipped. If either works, the answer is true.

Complexity

approachtimespace
Try every deletionO(n²)O(n)
Two pointersO(n)O(1)

More walkthroughs