Valid Palindrome II
Easy
Topics
Given a string s, return true if it can be a palindrome after deleting at most one character.
Example 1
true.Input: s = "aba" Output: true
Example 2
true.Input: s = "abca" Output: true
Example 3
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
- Two pointers inward while characters match.
- On a mismatch, return whether
s[i+1..j]ors[i..j-1]is a strict palindrome. - 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 falseComplexity
- 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 Trueimport java.util.*;
class Solution {
public boolean validPalindrome(String s) {
int i = 0, j = s.length() - 1;
while (i < j) {
if (s.charAt(i) != s.charAt(j)) return isPal(s, i + 1, j) || isPal(s, i, j - 1);
i++;
j--;
}
return true;
}
boolean isPal(String s, int i, int j) {
while (i < j) {
if (s.charAt(i) != s.charAt(j)) return false;
i++;
j--;
}
return true;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: bool validPalindrome(string s) {
auto isPal = [&](int i, int j) {
while (i < j) {
if (s[i] != s[j]) return false;
i++;
j--;
}
return true;
};
int i = 0, j = (int)s.size() - 1;
while (i < j) {
if (s[i] != s[j]) return isPal(i + 1, j) || isPal(i, j - 1);
i++;
j--;
}
return true;
}
};/**
* @param {string} s
* @return {boolean}
*/ var validPalindrome = function(s) {
const isPal = (i, j) => {
while (i < j) {
if (s[i] !== s[j]) return false;
i++;
j--;
}
return true;
};
let i = 0, j = s.length - 1;
while (i < j) {
if (s[i] !== s[j]) return isPal(i + 1, j) || isPal(i, j - 1);
i++;
j--;
}
return true;
};