题目大意

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

    解题思路

    双指针的方法解决

    1. class Solution:
    2. def validPalindrome(self, s: str) -> bool:
    3. def checkPalindrome(low, high):
    4. i, j = low, high
    5. while i < j:
    6. if s[i] != s[j]:
    7. return False
    8. i += 1
    9. j -= 1
    10. return True
    11. low, high = 0, len(s) - 1
    12. while low < high:
    13. if s[low] == s[high]:
    14. low += 1
    15. high -= 1
    16. else:
    17. return checkPalindrome(low + 1, high) or checkPalindrome(low, high - 1)
    18. return True