Overview
Given a string s, return true if it is a palindrome after converting all uppercase letters to lowercase and removing all non-alphanumeric characters. Otherwise return false.
Constraints
1 <= s.length <= 200,000sconsists of printable ASCII characters.- Only alphanumeric characters are considered for the palindrome check.
Examples
isPalindrome("A man, a plan, a canal: Panama");
// => true ("amanaplanacanalpanama")isPalindrome("race a car");
// => false
isPalindrome(" ");
// => true (empty after stripping → trivially a palindrome)Solution
Reveal solution
function isPalindrome(s) {
const clean = s.toLowerCase().replace(/[^a-z0-9]/g, "");
let l = 0, r = clean.length - 1;
while (l < r) {
if (clean[l] !== clean[r]) return false;
l++;
r--;
}
return true;
}Resources
string-palindrome.js
String Palindrome
easycodingAlgorithmsStrings
Overview
Given a string s, return true if it is a palindrome after converting all uppercase letters to lowercase and removing all non-alphanumeric characters. Otherwise return false.
Constraints
1 <= s.length <= 200,000sconsists of printable ASCII characters.- Only alphanumeric characters are considered for the palindrome check.
Examples
isPalindrome("A man, a plan, a canal: Panama");
// => true ("amanaplanacanalpanama")isPalindrome("race a car");
// => false
isPalindrome(" ");
// => true (empty after stripping → trivially a palindrome)Solution
Reveal solution
function isPalindrome(s) {
const clean = s.toLowerCase().replace(/[^a-z0-9]/g, "");
let l = 0, r = clean.length - 1;
while (l < r) {
if (clean[l] !== clean[r]) return false;
l++;
r--;
}
return true;
}Resources
NameTopicDifficulty
103 of 103 problems