Valid Palindrome II - Problem

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

A palindrome reads the same forward and backward. For example, "racecar" and "level" are palindromes.

Your task is to determine if removing zero or one character can make the string a palindrome.

Input & Output

Example 1 — Can Make Palindrome
$ Input: s = "aba"
Output: true
💡 Note: "aba" is already a palindrome, so we return true without deleting any character
Example 2 — Need One Deletion
$ Input: s = "abca"
Output: true
💡 Note: We can delete 'c' to get "aba" or delete 'b' to get "aca", both are palindromes
Example 3 — Cannot Make Palindrome
$ Input: s = "abc"
Output: false
💡 Note: No matter which character we delete, we cannot make it a palindrome with just one deletion

Constraints

  • 1 ≤ s.length ≤ 105
  • s consists of lowercase English letters only

Visualization

Tap to expand
Valid Palindrome II: Check if Palindrome After ≤1 DeletionInputabca"abca"ProcessTwo Pointers Check:a = a ✓, but b ≠ c ✗Try: Skip 'b' → "aca" ✓Or: Skip 'c' → "aba" ✓OutputtrueCan make palindromeAlgorithm Steps:1. Use left and right pointers from ends2. If characters match, move pointers inward3. If mismatch, try skipping left OR right character4. Check if remaining substring is palindrome
Understanding the Visualization
1
Input
String that might need one character removed
2
Process
Use two pointers to find mismatches efficiently
3
Output
Return true if palindrome possible with ≤1 deletion
Key Takeaway
🎯 Key Insight: When two pointers find a mismatch, try skipping either character - if either option creates a palindrome, return true!
Asked in
Facebook 45 Microsoft 38 Amazon 32 Google 28
125.0K Views
High Frequency
~15 min Avg. Time
3.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen