Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript - Find if string is a palindrome (Check for punctuation)
In JavaScript, checking if a string is a palindrome while handling punctuation requires cleaning the string first. A palindrome reads the same forwards and backwards, ignoring spaces, punctuation, and case.
What is a Palindrome?
A palindrome is not limited to single words like "radar" or "level"; it can include phrases or sequences like "Madam, in Eden, I'm Adam." We need to ignore punctuation, spaces, and case differences to ensure accurate palindrome checks.
Using String and Array Methods
The first approach uses JavaScript's built-in string and array methods to normalize and reverse the string:
- The toLowerCase() method converts all characters to lowercase
- Regular expressions remove non-alphanumeric characters
- The reverse() method reverses the array of characters
- The join() method combines array elements back into a string
Example
function isPalindrome(str) {
// Step 1: Convert to lowercase
const lowerCaseStr = str.toLowerCase();
// Step 2: Remove punctuation and spaces using regex
const modifiedStr = lowerCaseStr.replace(/[\W_]/g, '');
// Step 3: Reverse the cleaned string
const reversedStr = modifiedStr.split('').reverse().join('');
// Step 4: Compare original cleaned string with reversed
return modifiedStr === reversedStr;
}
// Test with punctuation
const inputString = "A man, a plan, a canal, Panama!";
console.log(isPalindrome(inputString));
console.log(isPalindrome("race a car")); // false
console.log(isPalindrome("Was it a car or a cat I saw?")); // true
true false true
Using Two-Pointer Technique
For better efficiency, the two-pointer technique compares characters from both ends simultaneously without creating a reversed string:
function isPalindromeTwoPointer(str) {
// Clean string: remove punctuation, spaces, and convert to lowercase
const cleanedStr = str.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
let left = 0;
let right = cleanedStr.length - 1;
while (left < right) {
if (cleanedStr[left] !== cleanedStr[right]) {
return false;
}
left++;
right--;
}
return true;
}
// Test examples
console.log(isPalindromeTwoPointer("A man, a plan, a canal, Panama"));
console.log(isPalindromeTwoPointer("Not a palindrome!"));
console.log(isPalindromeTwoPointer("Madam, I'm Adam"));
true false true
Comparison
| Method | Space Complexity | Time Complexity | Memory Usage |
|---|---|---|---|
| String Reversal | O(n) | O(n) | Creates new strings |
| Two-Pointer | O(1) | O(n) | More memory efficient |
Key Points
- Both methods handle punctuation using regex pattern
/[^a-zA-Z0-9]/g - Case insensitivity is achieved with
toLowerCase() - Two-pointer technique is more memory efficient for large strings
- String reversal method is simpler to understand and implement
Conclusion
Both approaches effectively check palindromes with punctuation. Use string reversal for simplicity or two-pointer technique for better memory efficiency with large inputs.
