Home
DSA Patterns
Two Pointers
Valid Palindrome II
Valid Palindrome II (LeetCode 680)
Valid Palindrome II (LeetCode 680): decide whether a string becomes a palindrome after deleting at most one character, by trying both deletions only at the first mismatch.
Problem Understanding
Valid Palindrome II: return true if s can be a palindrome after deleting at most one character.
Examples: "aba" is already a palindrome; "abca" becomes "aba" or "aca" by deleting one character; "abc" cannot be fixed with one deletion.
The Intuition: Spend the Deletion at the First Mismatch
Check from the outside in, as in Valid Palindrome, with two pointers l and r. While s[l] == s[r], those characters are fine and both pointers move inward.
At the first mismatch, one of s[l] or s[r] must be the extra character, since every pair outside already matches. So there are only two candidates:
- Delete
s[l]: iss[l+1..r]a palindrome? - Delete
s[r]: iss[l..r−1]a palindrome?
If either is, return true. If the pointers meet without a mismatch, no deletion was needed.
Valid Palindrome II Visualization
AT MOST ONE CHARACTER MAY BE DELETED
l AND r AT THE ENDS
l
0
1
2
r
3
l
0
r
3
Answer
:
l at the start, r at the end of "abca"
Next
s[0] = 'a' matches s[3]: move both inward
s
Matched pairs are shaded from the outside in. At the first mismatch, watch each deletion get checked in turn: the deleted character is struck through and the rest is compared from both ends.
Valid Palindrome II Dry Run
Input: "abca"
| l | r | Compare | Action |
|---|---|---|---|
| 0 | 3 | a = a | move in |
| 1 | 2 | b ≠ c | first mismatch |
| - | - | delete b: "c" | palindrome |
Return true.
Valid Palindrome II LeetCode Solution
Two pointers, with a palindrome check for each possible deletion.
Valid Palindrome II Code
1var validPalindrome = function(s) {2 const isPal = (l, r) => {3 while (l < r) {4 if (s[l++] !== s[r--]) return false;5 }6 return true;7 };8 let l = 0, r = s.length - 1;9 while (l < r) {10 if (s[l] !== s[r]) return isPal(l + 1, r) || isPal(l, r - 1);11 l++;12 r--;13 }14 return true;15};
Valid Palindrome II in Python, Java, C++, TypeScript and C
Valid Palindrome II in Python
class Solution:
def validPalindrome(self, s: str) -> bool:
def is_pal(l, r):
while l < r:
if s[l] != s[r]:
return False
l, r = l + 1, r - 1
return True
l, r = 0, len(s) - 1
while l < r:
if s[l] != s[r]:
return is_pal(l + 1, r) or is_pal(l, r - 1)
l, r = l + 1, r - 1
return TrueValid Palindrome II in Java
class Solution {
public boolean validPalindrome(String s) {
int l = 0, r = s.length() - 1;
while (l < r) {
if (s.charAt(l) != s.charAt(r)) return isPal(s, l + 1, r) || isPal(s, l, r - 1);
l++;
r--;
}
return true;
}
private boolean isPal(String s, int l, int r) {
while (l < r) {
if (s.charAt(l++) != s.charAt(r--)) return false;
}
return true;
}
}Valid Palindrome II in C++
class Solution {
bool isPal(const string& s, int l, int r) {
while (l < r) {
if (s[l++] != s[r--]) return false;
}
return true;
}
public:
bool validPalindrome(string s) {
int l = 0, r = s.size() - 1;
while (l < r) {
if (s[l] != s[r]) return isPal(s, l + 1, r) || isPal(s, l, r - 1);
l++;
r--;
}
return true;
}
};Valid Palindrome II in TypeScript
function validPalindrome(s: string): boolean {
const isPal = (l: number, r: number): boolean => {
while (l < r) {
if (s[l++] !== s[r--]) return false;
}
return true;
};
let l = 0, r = s.length - 1;
while (l < r) {
if (s[l] !== s[r]) return isPal(l + 1, r) || isPal(l, r - 1);
l++;
r--;
}
return true;
}Valid Palindrome II in C
static bool isPal(char* s, int l, int r) {
while (l < r) {
if (s[l++] != s[r--]) return false;
}
return true;
}
bool validPalindrome(char* s) {
int l = 0, r = strlen(s) - 1;
while (l < r) {
if (s[l] != s[r]) return isPal(s, l + 1, r) || isPal(s, l, r - 1);
l++;
r--;
}
return true;
}Edge Cases & Common Mistakes
- Already a palindrome: no deletion needed; return true.
- Trying only one side: "eeeed" fails if you only delete the left character; check both.
- Allowing more than one deletion: once you are inside a check, any further mismatch means false.
- Re-checking the whole string: only the part between l and r needs checking after the first mismatch.
Summary
Move two pointers inward while the ends match. At the first mismatch, the deleted character must be s[l] or s[r], so return whether s[l+1..r] or s[l..r−1] is a palindrome. If there is no mismatch, return true. O(n) time, O(1) space.
More practice: DSA interview questions with answers · patterns cheat sheet
