reference solution
class Solution:
def isPalindrome(self, s: str) -> bool:
n = len(s)
left, right = 0, n - 1
while left < right: # Note(1)
while left < right and not s[left].isalnum(): # Note(2)
left += 1
while left < right and not s[right].isalnum(): # Note(2)
right -= 1
print(left, right)
if left < right: # Note(3)
if s[left].lower() != s[right].lower(): # Note(5)
return False
left, right = left + 1, right - 1 # Note(4)
print(left, right)
return True
The part that is easy to overlook in the topic is:
Note(1) and Note(3) can be used together, because Note(4) guarantees that it will left < right
not happenleft <= right
left==right
Note (2) Here, python or other programming languages have built-in functions for judging whether a string contains only letters or numbers~ I learned and learned
isalnum() to judge whether a string contains only letters or numbers
isalpha() judgment Whether the string contains only letters
isdigit() checks whether the string is only composed of numbers
Note(5) 字符.lower()
When the characters here are letters or numbers, the upper and lower case can be judged.