LeetCode Problem

How to Solve Valid Palindrome II

To solve this problem, check if a string can be a palindrome by deleting at most one character. Use a two-pointer approach, comparing characters from both ends and allowing one deletion to fix mismatches.

GhostInterview Help

Need help with Valid Palindrome II without spending extra time grinding it?

GhostInterview can read Valid Palindrome II from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #680Two-pointer scanning with invariant trackingReviewed 2026-03-08
Difficulty
Easy
Primary pattern
Two-pointer scanning with invariant tracking
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve this problem, check if a string can be a palindrome by deleting at most one character. Use a two-pointer approach, comparing characters from both ends and allowing one deletion to fix mismatches.

Problem Statement

Given a string s, return true if it can be a palindrome after deleting at most one character. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward.

Implement a solution where you use two pointers to check if characters at both ends are equal. If they are not, try removing one character at either pointer and check if the result is a palindrome.

Examples

Example 1

Input: s = "aba"

Output: true

Example details omitted.

Example 2

Input: s = "abca"

Output: true

You could delete the character 'c'.

Example 3

Input: s = "abc"

Output: false

Example details omitted.

Constraints

  • 1 <= s.length <= 105
  • s consists of lowercase English letters.

Solution Approach

Two-pointer scanning

Start by using two pointers at the beginning and the end of the string. If the characters at these positions match, move both pointers inward. If they don’t match, check if removing one character at either position allows the string to become a palindrome.

Invariant tracking

Track the invariant that the string must be a palindrome after removing one character. At each mismatch, ensure that deleting a character at either pointer creates a valid palindrome for the remaining string.

Greedy approach

Apply a greedy method to remove a character at the first mismatch. Check if skipping one character creates a valid palindrome. If not, return false immediately.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity is O(n) because each pointer moves across the string at most once. The space complexity is O(1), as the solution uses constant extra space beyond the input string.

What Interviewers Usually Probe

  • Candidate should demonstrate a clear understanding of two-pointer techniques.
  • Look for how well the candidate handles the one-character deletion requirement.
  • Evaluate the ability to optimize and keep the solution space-efficient.

Common Pitfalls or Variants

Common pitfalls

  • Not handling the case when characters match early but fail when removing one.
  • Over-complicating the solution by using extra data structures like arrays or sets.
  • Missing edge cases like strings that are already palindromes or too short to modify.

Follow-up variants

  • Check if the string can be a palindrome with two or more deletions.
  • Use this technique with longer strings for performance optimization.
  • Consider case sensitivity when expanding the problem scope.

How GhostInterview Helps

  • GhostInterview helps identify and simulate the core failure mode of allowing only one character removal for palindrome validation.
  • The solver walks through the two-pointer approach and highlights common missteps, ensuring efficient problem solving.
  • It also aids in testing the solution with edge cases, ensuring the candidate is well-prepared for interview settings.

Topic Pages

FAQ

What is the time complexity of Valid Palindrome II?

The time complexity is O(n) because the two-pointer technique scans the string once.

How does the two-pointer approach work for this problem?

The two-pointer approach compares characters from both ends and moves inward, allowing for one character deletion if a mismatch occurs.

What is the maximum length of string s in Valid Palindrome II?

The maximum length of s is 10^5, which requires an efficient O(n) solution.

Can Valid Palindrome II handle strings that are already palindromes?

Yes, if the string is already a palindrome, the solution will return true without any deletions.

What is the space complexity of Valid Palindrome II?

The space complexity is O(1), as the solution uses only constant extra space beyond the input string.

GhostInterview Solver

Need direct help with Valid Palindrome II instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Valid Palindrome II from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.