Below is the implementation of the palindrome checker:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869/** * Checks if a string is a palindrome (reads the same forward and backward). * Ignores non-alphanumeric characters and is case-insensitive. * * @param {string} s - The input string to check * @return {boolean} - True if the string is a palindrome, false otherwise */function isPalindrome(s) { // Initialize two pointers let left = 0; let right = s.length - 1; while (left < right) { // Skip non-alphanumeric characters from left while (left < right && !isAlphanumeric(s[left])) { left++; } // Skip non-alphanumeric characters from right while (left < right && !isAlphanumeric(s[right])) { right--; } // Compare characters (case-insensitive) if (s[left].toLowerCase() !== s[right].toLowerCase()) { return false; } // Move pointers toward each other left++; right--; } // If we get here, the string is a palindrome return true;} /** * Helper function to check if a character is alphanumeric */function isAlphanumeric(char) { const code = char.charCodeAt(0); return ( (code >= 48 && code <= 57) || // 0-9 (code >= 65 && code <= 90) || // A-Z (code >= 97 && code <= 122) // a-z );} // Alternative implementation using built-in methodsfunction isPalindromeAlt(s) { // Clean the string: remove non-alphanumeric and convert to lowercase const cleaned = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); // Check if the cleaned string equals its reverse const reversed = cleaned.split('').reverse().join(''); return cleaned === reversed;} // Test casesconsole.log(isPalindrome("racecar")); // trueconsole.log(isPalindrome("hello")); // falseconsole.log(isPalindrome("A man, a plan, a canal: Panama")); // trueconsole.log(isPalindrome("")); // true (empty string is a palindrome) console.log(isPalindromeAlt("racecar")); // trueconsole.log(isPalindromeAlt("hello")); // falseconsole.log(isPalindromeAlt("A man, a plan, a canal: Panama")); // trueconsole.log(isPalindromeAlt("")); // true
Let's break down the implementation:
Implement the palindrome checker solution in different programming languages.
Below is the implementation of the palindrome checker in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869/** * Checks if a string is a palindrome (reads the same forward and backward). * Ignores non-alphanumeric characters and is case-insensitive. * * @param {string} s - The input string to check * @return {boolean} - True if the string is a palindrome, false otherwise */function isPalindrome(s) { // Initialize two pointers let left = 0; let right = s.length - 1; while (left < right) { // Skip non-alphanumeric characters from left while (left < right && !isAlphanumeric(s[left])) { left++; } // Skip non-alphanumeric characters from right while (left < right && !isAlphanumeric(s[right])) { right--; } // Compare characters (case-insensitive) if (s[left].toLowerCase() !== s[right].toLowerCase()) { return false; } // Move pointers toward each other left++; right--; } // If we get here, the string is a palindrome return true;} /** * Helper function to check if a character is alphanumeric */function isAlphanumeric(char) { const code = char.charCodeAt(0); return ( (code >= 48 && code <= 57) || // 0-9 (code >= 65 && code <= 90) || // A-Z (code >= 97 && code <= 122) // a-z );} // Alternative implementation using built-in methodsfunction isPalindromeAlt(s) { // Clean the string: remove non-alphanumeric and convert to lowercase const cleaned = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); // Check if the cleaned string equals its reverse const reversed = cleaned.split('').reverse().join(''); return cleaned === reversed;} // Test casesconsole.log(isPalindrome("racecar")); // trueconsole.log(isPalindrome("hello")); // falseconsole.log(isPalindrome("A man, a plan, a canal: Panama")); // trueconsole.log(isPalindrome("")); // true (empty string is a palindrome) console.log(isPalindromeAlt("racecar")); // trueconsole.log(isPalindromeAlt("hello")); // falseconsole.log(isPalindromeAlt("A man, a plan, a canal: Panama")); // trueconsole.log(isPalindromeAlt("")); // true
Initialize two pointers at the beginning and end of the string.
Skip non-alphanumeric characters as we move the pointers.
Compare characters at both pointers (case-insensitively).
If characters don't match, return false.
If pointers meet in the middle without mismatches, return true.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the palindrome checker problem using a different approach than shown above.
By definition, an empty string is a palindrome.
Any single character is a palindrome.
After removing non-alphanumeric characters, we'd have an empty string, which is a palindrome.
Remember to compare characters case-insensitively.
Below is the implementation of the palindrome checker:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869/** * Checks if a string is a palindrome (reads the same forward and backward). * Ignores non-alphanumeric characters and is case-insensitive. * * @param {string} s - The input string to check * @return {boolean} - True if the string is a palindrome, false otherwise */function isPalindrome(s) { // Initialize two pointers let left = 0; let right = s.length - 1; while (left < right) { // Skip non-alphanumeric characters from left while (left < right && !isAlphanumeric(s[left])) { left++; } // Skip non-alphanumeric characters from right while (left < right && !isAlphanumeric(s[right])) { right--; } // Compare characters (case-insensitive) if (s[left].toLowerCase() !== s[right].toLowerCase()) { return false; } // Move pointers toward each other left++; right--; } // If we get here, the string is a palindrome return true;} /** * Helper function to check if a character is alphanumeric */function isAlphanumeric(char) { const code = char.charCodeAt(0); return ( (code >= 48 && code <= 57) || // 0-9 (code >= 65 && code <= 90) || // A-Z (code >= 97 && code <= 122) // a-z );} // Alternative implementation using built-in methodsfunction isPalindromeAlt(s) { // Clean the string: remove non-alphanumeric and convert to lowercase const cleaned = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); // Check if the cleaned string equals its reverse const reversed = cleaned.split('').reverse().join(''); return cleaned === reversed;} // Test casesconsole.log(isPalindrome("racecar")); // trueconsole.log(isPalindrome("hello")); // falseconsole.log(isPalindrome("A man, a plan, a canal: Panama")); // trueconsole.log(isPalindrome("")); // true (empty string is a palindrome) console.log(isPalindromeAlt("racecar")); // trueconsole.log(isPalindromeAlt("hello")); // falseconsole.log(isPalindromeAlt("A man, a plan, a canal: Panama")); // trueconsole.log(isPalindromeAlt("")); // true
Let's break down the implementation:
Implement the palindrome checker solution in different programming languages.
Below is the implementation of the palindrome checker in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869/** * Checks if a string is a palindrome (reads the same forward and backward). * Ignores non-alphanumeric characters and is case-insensitive. * * @param {string} s - The input string to check * @return {boolean} - True if the string is a palindrome, false otherwise */function isPalindrome(s) { // Initialize two pointers let left = 0; let right = s.length - 1; while (left < right) { // Skip non-alphanumeric characters from left while (left < right && !isAlphanumeric(s[left])) { left++; } // Skip non-alphanumeric characters from right while (left < right && !isAlphanumeric(s[right])) { right--; } // Compare characters (case-insensitive) if (s[left].toLowerCase() !== s[right].toLowerCase()) { return false; } // Move pointers toward each other left++; right--; } // If we get here, the string is a palindrome return true;} /** * Helper function to check if a character is alphanumeric */function isAlphanumeric(char) { const code = char.charCodeAt(0); return ( (code >= 48 && code <= 57) || // 0-9 (code >= 65 && code <= 90) || // A-Z (code >= 97 && code <= 122) // a-z );} // Alternative implementation using built-in methodsfunction isPalindromeAlt(s) { // Clean the string: remove non-alphanumeric and convert to lowercase const cleaned = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); // Check if the cleaned string equals its reverse const reversed = cleaned.split('').reverse().join(''); return cleaned === reversed;} // Test casesconsole.log(isPalindrome("racecar")); // trueconsole.log(isPalindrome("hello")); // falseconsole.log(isPalindrome("A man, a plan, a canal: Panama")); // trueconsole.log(isPalindrome("")); // true (empty string is a palindrome) console.log(isPalindromeAlt("racecar")); // trueconsole.log(isPalindromeAlt("hello")); // falseconsole.log(isPalindromeAlt("A man, a plan, a canal: Panama")); // trueconsole.log(isPalindromeAlt("")); // true
Initialize two pointers at the beginning and end of the string.
Skip non-alphanumeric characters as we move the pointers.
Compare characters at both pointers (case-insensitively).
If characters don't match, return false.
If pointers meet in the middle without mismatches, return true.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the palindrome checker problem using a different approach than shown above.
By definition, an empty string is a palindrome.
Any single character is a palindrome.
After removing non-alphanumeric characters, we'd have an empty string, which is a palindrome.
Remember to compare characters case-insensitively.