125 验证回文串

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

说明:本题中,我们将空字符串定义为有效的回文串。

示例 1:

输入: "A man, a plan, a canal: Panama"
输出: true

示例 2:

输入: "race a car"
输出: false

解法

  • 先清洗数据,再反转数据和原数据是否一致。
  • 双指针法
class Solution:
    def isPalindrome(self, s: str) -> bool:
        def is_a1(ss):
            if 'a'<=ss<='z'or'A'<=ss<='Z'or '0'<=ss<='9':
                return True
            return False

        left,right=0,len(s)-1
        while left<right:
            while left<right and (not is_a1(s[left])):
                left += 1
            while left<right and ( not is_a1(s[right])):
                right -= 1

            if s[left].lower()!=s[right].lower():
                return False
            else:
                left += 1
                right -= 1
        return True