# 验证回文子串

  1. 验证回文子串II

来源:力扣(LeetCode) 链接 (opens new window):https://leetcode.cn/problems/valid-palindrome-ii/

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

github (opens new window)

# 问题

给定一个非空字符串 s,最多删除一个字符。判断是否能成为回文字符串

输入: s = "aba"
输出: true

# 思路

function isValidPalindrome(string) {
  for (let i = 0; i < string.length; i++) {
    const first = string[i];
    const last = string[string.length - 1 - i];
    if (first !== last) {
      return false;
    }
  }
  return true;
}

var validPalindrome = function (s) {
  let left = 0;
  let right = s.length - 1;
  while (left < right) {
    if (s[left] === s[right]) {
      left++;
      right--;
    } else {
      return (
        isValidPalindrome(s.slice(left + 1, right + 1)) ||
        isValidPalindrome(s.slice(left, right))
      );
    }
  }

  return true;
};
陕ICP备20004732号-3