Problem
Given a string s, return true if the s can be palindrome after deleting at most one character from it.
Examples
Example 1:
| |
Example 2:
| |
Example 3:
| |
Solution
Method 1 - Using Two Pointers
We can use the old approach like in Valid Palindrome. But with some modification.
For eg. take aaaz; Now when we compare left and right values, i.e. a and z, we can see they are different. But it is fine, as 1 char can be deleted. So, should it be z OR a. So, when we remove z, we get it is as palindrome. So, we have to try out both aaa and aaz to see if they are palindrome.
Code
| |
Complexity
- ⏰ Time complexity:
O(n) - 🧺 Space complexity:
O(1)