## Leecod: verifying palindrome string (Python)

A good function 2020-11-13 02:57:10
leecod verifying palindrome string python

## 1. Title Description

Given a string , Verify that it is a palindrome string , Consider only alphabetic and numeric characters , The case of letters can be ignored .
explain ： In this question , We define an empty string as a valid palindrome string .

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

Example 2:
Input : “race a car”
Output : false

## 2. Ideas

Use a double pointer , A former and a later left The pointer , One from the back to the front right The pointer , Check left and right Whether it is equal or not , Continue if equal , If not equal return False. You need to change the upper case of the string to lower case before checking . In order to unify case , Convenient comparison . in addition , Because there may be spaces and punctuation in the string , So you need to check whether the character the pointer points to is a number or a letter , If not , Just skip. .

### 2.1 python Code

``````class Solution:
def isPalindrome(self, s: str) -> bool:
if len(s) == 0:
return True
s = s.lower()
left = 0
right = len(s) - 1
while left < right:
if (s[left] in "1234567890qwertyuioplkjhgfdsazxcvbnm" and
s[right] in "1234567890qwertyuioplkjhgfdsazxcvbnm"):
if s[left] == s[right]:
left += 1
right -= 1
else:
return False
else:
if s[left] not in "1234567890qwertyuioplkjhgfdsazxcvbnm":
left += 1
elif s[right] not in "1234567890qwertyuioplkjhgfdsazxcvbnm":
right -= 1
return True
``````