AlgoInsight

DSA problem

Valid Palindrome II

EasyStringsPattern: Two Pointerstop600FacebookAmazonMicrosoft

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

The string consists of lowercase English letters only, so no case folding or character filtering is required. Deleting zero characters is allowed, so any string that is already a palindrome trivially satisfies the condition.

The challenge is to decide, when a mismatch is found, whether skipping the left character or the right character can still yield a palindrome.

Examples

Example 1
Input: s = "aba"
Output: true
Explanation: "aba" is already a palindrome, so zero deletions are needed.
Example 2
Input: s = "abca"
Output: true
Explanation: Deleting the character 'c' yields "aba", which is a palindrome.
Example 3
Input: s = "abc"
Output: false
Explanation: No single deletion can make "abc" a palindrome.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

stringtwo-pointersgreedy

Browse all DSA problems