requirements

Given a string, verify that it is a palindrome string, considering only alphanumeric characters, regardless of letter case.

Note: In this case, we define an empty string as a valid palindrome string.

Example 1:

Input: "A man, A plan, A canal: Panama" Output: true explanation: "Amanaplanacanalpanama" is A palindromeCopy the code

Example 2:

Input: "raceacar" Output: false Explanation: "raceacar" is not a palindromeCopy the code

The core code

class Solution:
    def isPalindrome(self, s: str) - >bool:
        tmp = ""
        for char in s.lower():
            if char.isdigit() or char.isalpha():
                tmp += char
        return tmp == tmp[::-1]
Copy the code

We prepare an empty string, check for numbers and letters, put the numbers and letters in a new string, and reverse the string.