JavaScript - Find if string is a palindrome (Check for punctuation)


In the given problem statement we have to find that the string is a palindrome and the string should also have punctuation and write code with the help of Javascript functionalities.

Understanding the problem

The problem at hand is to explore how to determine whether a given string is a palindrome or not with the help of Javascript. So let's see what palindrome is first. A palindrome is a word, phrase, a number or a sequence of letters which can be read the same forward and backward. For example: racecar is a palindrome string.

Logic for the given problem

To solve the given problem we will first create a function for finding whether the given string is a palindrome or not. For checking that the string is a palindrome and also ignoring the punctuation we can convert the given input string to lowercase first for ignoring the case sensitivity. And after that we will remove all the punctuation marks and also space from the given string with the help of regular expressions or string manipulation methods. After that we will compare the updated string with the reverse of it. And if they are similar then we will return that the string is palindrome.

Algorithm

Step 1: As we have to find out if the given string is a palindrome or not. So for doing this task we will create a function called isPalindrome and in this function we will pass a parameter of string as str. So for this str we will check the palindrome condition.

Step 2: After the creation of the function we will convert the given string into lowercase with the help of toLowerCase method and then we will assign it to the new variable called lowerCaseStr.

Step 3: Now we have converted the string in lowercase, it is time to remove the spaces and punctuations from the given string with the help of regular expressions and string manipulation methods. After this we will assign it to another variable called modifiedStr.

Step 4: In this step we will reverse the given string with the help of split, reverse and join methods and then we will assign it to a new variable called reversedStr.

Step 5: As we have already converted the string in lowercase and also reversed the given string. Now we will compare the updated string with the reversed string with the help of === operator. And if they are equal then we will return true otherwise return false.

Example

// Function to check for Palindrome
function isPalindrome(str) {
   const lowerCaseStr = str.toLowerCase();
   const modifiedStr = lowerCaseStr.replace(/[\W_]/g, '');
   const reversedStr = modifiedStr.split('').reverse().join('');

   return modifiedStr === reversedStr;
}
const inputString = "A man, a plan, a canal, Panama!";
console.log(isPalindrome(inputString));

Output

true

Complexity

The time complexity for finding whether the given input string is palindrome or not is O(n), in which n is the size of the given input string. As we have used regular expression and string manipulation techniques which require linear time and also modification for the string it also takes linear time.

Conclusion

In the given task we have learned how to check that a string is a palindrome and ignore the punctuation using Javascript. We have done this task by converting the string to lowercase, removing punctuation marks and spaces. And also comparing both original and reversed strings we can determine that the given string is a palindrome or not.

Updated on: 14-Aug-2023

453 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements