LeetCode125 title
Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter case.
Description: In this problem, we define the empty string as a valid palindromic sequence.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/valid-palindrome
Method a: python built-in functions
class Solution:
def isPalindrome(self, s: str) -> bool:
s_filter = ' '.join(filter(str.isalnum, s)).lower()
return s_filter[::-1] == s_filter
Python isalnum () method
Whether Python isalnum () method for detecting a string of letters and numbers. Usage: str.isalnum ()
reference links https://www.runoob.com/python/att-string-isalnum.html
filter () function
filter () function is used to filter sequences, filtered ineligible element returns an iterator object, if you want to convert to the list, you can use list () to convert.
The receiving two parameters, as a function of a first, a second sequence, each element of the sequence as an argument to a function arbitrates, then return True or False, and finally returns True new elements into the list.
filter(function, iterable)
Reference link: https: //www.runoob.com/python3/python3-func-filter.html
.join () function
. Join (): connecting string array. A string, a tuple, in the list element to specify character (separator) connected to generate a new string
'' .join () separated by spaces
','. Join () separated by commas
Reference link: https: //www.cnblogs.com/ling-yu/p/9167065.html
List [:: -1]
b = a [i: j: s]
represented by: copying a [i] to a [j-1], to generate a new list object, but represents a step s, a default. Therefore, a [i: j: 1] corresponds to a [i: j]
when s <0, i is the default, default -1 j default, default -len (a) -1.
# Therefore a [:: --1] corresponds to a [-1: -len (a) -1: -1], that is copied from the last element to the first element again, i.e. reverse.
Reference link: https: //www.cnblogs.com/neozheng/p/11219867.html
Method two: Finger bis (collision pointer)
Wrong Solutions
class Solution:
def isPalindrome(self, s: str) -> bool:
result = True
left = 0
right = len(s)-1
while left < right:
if not s[left].isalnum():
left += 1
continue
if not s[right].isalnum():
right -= 1
continue
if s[left].lower() != s[right].lower():
result = False
else:
left += 1
right -= 1
return result
Modify:
class Solution:
def isPalindrome(self, s: str) -> bool:
result = True
left = 0
right = len(s)-1
while left < right:
if not s[left].isalnum():
left += 1
continue
if not s[right].isalnum():
right -= 1
continue
if s[left].lower() != s[right].lower():
result = False
left += 1
right -= 1
else:
left += 1
right -= 1
return result
Another way:
class Solution:
def isPalindrome(self, s: str) -> bool:
left = 0
right = len(s)-1
while left < right:
if not s[left].isalnum():
left += 1
continue
if not s[right].isalnum():
right -= 1
continue
if s[left].lower() != s[right].lower():
return False
else:
left += 1
right -= 1
return True