Problem 55
Valid Palindrome II
Medium · 20 pts · String, Two Pointers, Greedy
Problem
You are given a string S. Decide whether S can be made into a palindrome (a string that reads the same forwards and backwards) by deleting at most one character. A string that is already a palindrome needs no deletion, so the answer for it is YES. For example, "abca" becomes the palindrome "aba" after deleting c. Use two pointers from both ends. At the first mismatch, you have only one deletion, so try skipping the left character or the right one, and check whether the rest is a palindrome.
Input
A single string S of lowercase English letters.
Output
Print YES or NO.
Constraints
1 <= |S| <= 100000
Example input
aba
Example output
YES
Explanation
"aba" already reads the same forwards and backwards, so no deletion is needed and the answer is YES.