I. Title Description:
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
Copy the code
Example 2:
Input:"race a car"Output:false
Copy the code
Ii. Analysis of Ideas:
1. Validate valid characters and store them in lowercase or uppercase
2. Compare the first element with the double pointer, and judge it as palindrome string if all corresponding positions are the same
Iii. AC Code:
javascript
/ * * *@param {string} s
* @return {boolean}* /
var isPalindrome = function (s) {
let isValidElem = (s) = > {
let str = 'qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM' // A valid character
return str.indexOf(s) > -1 // Check if it is a valid character
}
let stack = [] // Store a valid string
for (let i = 0; i < s.length; i++) {
if (isValidElem(s[i])) {
stack.push(s[i].toLocaleLowerCase()) // Lowercase letters store valid strings}}let left = 0, right = stack.length - 1; / / double pointer
while (left < right) {
if(stack[left] ! == stack[right])return false
left++ / / pointer to the left
right-- / / right pointer
}
return true
};
Copy the code
python
class Solution(object) :def isPalindrome(self.s) : "" ":type s: str
:rtype: bool"" "def isValidElem(self.s1): ## checks if it is a valid stringreturn '0' <= s1 <= '9' or 'a' <= s1 <= 'z' or 'A' <= s1 <= 'Z'Stack = [] ## Stores valid stringsfor i in range(len(s)):
ifIsValidElem (self,s[I]): stack.appEnd (s[I].lower()) #0, len(stack)-1Double pointer traversalwhile left < right:
ifstack[left] ! = stack[right]: ##return False
left +=1
right-=1
return True
Copy the code
This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details