Valid Palindrome II

Easy

Topics
StringTwo PointersGreedy

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

Example 1

a0b1a2
The string is already a palindrome, so no deletion is needed and the answer is true.
Cells are the characters of the string; the small numbers underneath are indices.
Input:  s = "aba"
Output: true

Example 2

a0b1c2a3
first mismatch
At that single point the one allowed deletion must be spent, so the only question left is whether skipping the left character or the right one leaves a palindrome — two linear checks, no branching search. Answer: true.
Input:  s = "abca"
Output: true

Example 3

a0b1c2
first mismatch
Input:  s = "abc"
Output: false

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase English letters.
Editorial — intuition, approach, complexity and pitfalls

Intuition

Walk two pointers inward. While the characters match there is nothing to decide. At the first mismatch we may delete one character, and there are exactly two ways to spend it: drop the left one or drop the right one. Everything outside the current window already matched, so the rest of the string is irrelevant — the question reduces to whether either remaining window is a plain palindrome.

That gives the shape: a linear scan, and at most one branch into two linear checks. Crucially the branch happens once, not recursively — the deletion budget is one, so neither sub-check may delete anything further.

Approach

  1. Two pointers inward while characters match.
  2. On a mismatch, return whether s[i+1..j] or s[i..j-1] is a strict palindrome.
  3. If the pointers meet, the string was already a palindrome.

Dry Run

s = "abca"

i=0 'a', j=3 'a'   match, move inward
i=1 'b', j=2 'c'   MISMATCH

  option 1: drop 'b' -> check "c"   palindrome  -> true
  (option 2 would drop 'c' -> check "b", also a palindrome)

answer true

s = "abc"
i=0 'a', j=2 'c'   mismatch
  drop 'a' -> "bc"  not a palindrome
  drop 'c' -> "ab"  not a palindrome
answer false

Complexity

  • Time — O(n): the outer scan is linear, and the two helper checks together add at most O(n) more. Not O(n^2) — the branch is taken at most once, which is the property to state.
  • Space — O(1): indices only, provided the helper compares in place rather than slicing out substrings. s[i+1:j+1] would allocate and make it O(n).

Common Pitfalls

  • Allowing a further deletion inside the helper, which solves the "delete at most k" problem instead and accepts strings it should reject.
  • Recursing on the whole predicate after a mismatch rather than calling a strict palindrome check.
  • Slicing substrings for the two checks, which costs O(n) space and undercuts the O(1) claim.
  • Testing only one of the two deletions. Both must be tried — "abca" succeeds either way, but "cbbcc"-style inputs do not.

Full Solution — all languages

class Solution:
    def validPalindrome(self, s):
        def isPal(i, j):
            while i < j:
                if s[i] != s[j]:
                    return False
                i += 1
                j -= 1
            return True
        i, j = 0, len(s) - 1
        while i < j:
            if s[i] != s[j]:
                return isPal(i + 1, j) or isPal(i, j - 1)
            i += 1
            j -= 1
        return True
Asked at Meta
SensAI
Beta
Tap the mic to stop voice mode