125. Verify palindrome strings
Answer:
- A palindrome string is a string that, when flipped, still equals itself.
- Take the letters and numbers from the input string (uppercase letters need to be converted to lowercase) and store them in an array.
- Array-based
reverse
Method to flip the string, so compare the before and after the flip.
/ * * *@param {string} s
* @return {boolean}* /
var isPalindrome = function (s) {
let arr = []; // Use arrays to store qualified characters
// Iterate over s and store qualified characters into ARR
for (const char of s) {
// Alphanumeric characters are eligible
if (char >= '0' && char <= '9') {
arr.push(char);
} else if (char >= 'a' && char <= 'z') {
arr.push(char);
}
// Case should be ignored. The upper case is converted to lower case
else if (char >= 'A' && char <= 'Z') { arr.push(char.toLowerCase()); }}// Convert the array to a string for comparison
return arr.join(' ') === arr.reverse().join(' ');
};
Copy the code