Question:
Given a non-empty string s
, you may delete at most one character. Judge whether you can make it a palindrome(回文).
Example:
Input: "aba"
Output: True
Input: "abca"
Output: True
Explanation: You could delete the character 'c'.
Solution:
/**
* @param {string} s
* @return {boolean}
*/
var validPalindrome = function(s) {
let left = 0,
right = s.length-1,
count = 0;
let vaild = function (s,left,right) {
while (left <= right) {
if(s.charAt(left) !== s.charAt(right)) {
return false;
}
left ++;
right --;
}
return true;
}
while (left <= right) {
if (s.charAt(left) !== s.charAt(right)) {
return vaild(s,left+1,right) || vaild(s,left,right-1);
}
left++;
right--;
}
return true;
};
Runtime: 96 ms, faster than 63.87% of JavaScript online submissions for Valid Palindrome II.