Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:
Input: "race a car"
Output: false

Constraints:

  • s consists only of printable ASCII characters.

**Solution in python:

class Solution:
    def isPalindrome(self, s: str) -> bool:
        i = 0
        j = len(s) - 1
        while i < j:
            while i < j and (not s[i].isalnum()):
                i+=1
            while i < j and (not s[j].isalnum()):
                j-=1
            if s[i].lower() != s[j].lower():
                return False
            else:
                i+=1
                j-=1
        return True
最后修改日期: 2021年1月14日

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。