Question:

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome(回文).

Example:

  1. Input: "aba"
  2. Output: True
  1. Input: "abca"
  2. Output: True
  3. Explanation: You could delete the character 'c'.

Solution:

  1. /**
  2. * @param {string} s
  3. * @return {boolean}
  4. */
  5. var validPalindrome = function(s) {
  6. let left = 0,
  7. right = s.length-1,
  8. count = 0;
  9. let vaild = function (s,left,right) {
  10. while (left <= right) {
  11. if(s.charAt(left) !== s.charAt(right)) {
  12. return false;
  13. }
  14. left ++;
  15. right --;
  16. }
  17. return true;
  18. }
  19. while (left <= right) {
  20. if (s.charAt(left) !== s.charAt(right)) {
  21. return vaild(s,left+1,right) || vaild(s,left,right-1);
  22. }
  23. left++;
  24. right--;
  25. }
  26. return true;
  27. };

Runtime: 96 ms, faster than 63.87% of JavaScript online submissions for Valid Palindrome II.