Problem summary: Given a string s, return true if the s can be palindrome after deleting at most one character from it.
Baseline thinking
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Two Pointers · Greedy
Example 1
"aba"
Example 2
"abca"
Example 3
"abc"
Related Problems
Valid Palindrome (valid-palindrome)
Valid Palindrome III (valid-palindrome-iii)
Valid Palindrome IV (valid-palindrome-iv)
Step 02
Core Insight
What unlocks the optimal approach
No official hints in dataset. Start from constraints and look for a monotonic or reusable state.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03
Algorithm Walkthrough
Iteration Checklist
Define state (indices, window, stack, map, DP cell, or recursion frame).
Apply one transition step and update the invariant.
Record answer candidate when condition is met.
Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04
Edge Cases
Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Upper-end input sizes
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05
Full Annotated Code
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #680: Valid Palindrome II
class Solution {
private char[] s;
public boolean validPalindrome(String S) {
this.s = S.toCharArray();
for (int i = 0, j = s.length - 1; i < j; ++i, --j) {
if (s[i] != s[j]) {
return check(i + 1, j) || check(i, j - 1);
}
}
return true;
}
private boolean check(int i, int j) {
for (; i < j; ++i, --j) {
if (s[i] != s[j]) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #680: Valid Palindrome II
func validPalindrome(s string) bool {
check := func(i, j int) bool {
for ; i < j; i, j = i+1, j-1 {
if s[i] != s[j] {
return false
}
}
return true
}
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
if s[i] != s[j] {
return check(i+1, j) || check(i, j-1)
}
}
return true
}
# Accepted solution for LeetCode #680: Valid Palindrome II
class Solution:
def validPalindrome(self, s: str) -> bool:
def check(i, j):
while i < j:
if s[i] != s[j]:
return False
i, j = i + 1, j - 1
return True
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return check(i, j - 1) or check(i + 1, j)
i, j = i + 1, j - 1
return True
// Accepted solution for LeetCode #680: Valid Palindrome II
impl Solution {
pub fn valid_palindrome(s: String) -> bool {
fn is_palindrome(s: &[u8]) -> Option<(usize, usize)> {
let (mut i, mut j) = (0, s.len() - 1);
while i < j {
if s[i] != s[j] {
return Some((i, j));
}
i += 1;
j -= 1;
}
None // is palindrome
}
let s = s.as_bytes().to_vec();
match is_palindrome(&s) {
Some((i, j)) => {
if is_palindrome(&s[i+1..=j]).is_none() {
return true;
}
if is_palindrome(&s[i..=j-1]).is_none() {
return true;
}
}
None => {
return true;
}
}
false
}
}
// Accepted solution for LeetCode #680: Valid Palindrome II
function validPalindrome(s: string): boolean {
const check = (i: number, j: number): boolean => {
for (; i < j; ++i, --j) {
if (s[i] !== s[j]) {
return false;
}
}
return true;
};
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
if (s[i] !== s[j]) {
return check(i + 1, j) || check(i, j - 1);
}
}
return true;
}
Step 06
Interactive Study Demo
Use this to step through a reusable interview workflow for this problem.
Press Step or Run All to begin.
Step 07
Complexity Analysis
Time
O(n)
Space
O(1)
Approach Breakdown
BRUTE FORCE
O(n²) time
O(1) space
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
TWO POINTERS
O(n) time
O(1) space
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Shortcut: Two converging pointers on sorted data → O(n) time, O(1) space.
Coach Notes
Common Mistakes
Review these before coding to avoid predictable interview regressions.
Moving both pointers on every comparison
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Using greedy without proof
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.