125. Valid Palindrome
Easy
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
Accepted 500,090 Submissions 1,458,933
Double pointer
class Solution:
def isPalindrome(self, s: str) -> bool:
if len(s) <= 1:
return True
s = s.upper()
l = 0
r = len(s) - 1
while l<r:
if not s[l].isalnum():
l += 1
elif not s[r].isalnum():
r -= 1
elif s[l] != s[r]:
return False
else:
l += 1
r -= 1
return True
PS:
Python string determination
Python isX string method
1.isupper()If the string has at least one letter, and all letters are capitalized, isupper () method returns a Boolean value True.
2.islower()
If there is at least one letter string, and all the letters are in lowercase, islower () method returns a boolean value True.
3kislf ()
If the string contains only letters, and the non-empty, return True.
4.isalnum ()
If the string contains only alphanumeric characters, and non-empty, return True.
5.isdecimal ()
If the string contains only numeric characters, and is non-empty, returns True.
6.isspace()
If the string contains only spaces, tabs and line feed, and non-empty, return True.
7.istitl A ()
If the string contains only begin with a capital letter followed by lowercase letters are the words, return
True. ----------------
Copyright: This article is CSDN blogger "guloutingfengyu" original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/guloutingfengyu/article/details/80089190