AAlgoLoopSpaced repetition for LeetCode
EASYTwo PointersLeetCode ↗

Valid Palindrome II

The key idea

Walk two pointers inward from both ends. As long as the characters match, the outside is a palindrome and you keep going. At the FIRST mismatch you have exactly one deletion to spend, and only two candidates make sense: drop the left char or drop the right char. Check whether the substring between them (skipping one side) is a plain palindrome — if either skip works, the answer is true.

Problem

Given a string s, return true if s can be a palindrome after deleting at most one character from it.

A palindrome reads the same forward and backward. You may delete zero or one character; deleting 0 is allowed when s is already a palindrome.

Constraints

Examples

Input: s = "aba" Output: true
Input: s = "abca" Output: true
Input: s = "abc" Output: false

Complexity

Time: O(n) Space: O(1)

See the full solution

410310
Step-by-step visualization
Start free →

More Two Pointers problems