Function to find out palindrome strings JavaScript


In this problem statement, our aim is to create a function to find out that the given string is palindrome with the help of Javascript functionalities. So for solving this problem first we need to understand the problem in simple terms.

Understanding the problem statement

We have given a string as an input string and our main aim is to check if the string is a palindrome string or not. If it is palindrome then return true otherwise return false.

What do you mean by palindrome?

In the given problem statement there is the usage of the word palindrome !! Let’s first understand the meaning of this word. Palindrome is the term used when we have to define a string which looks the same after reversing it. For example we have a string “MADAM”, if we reverse this string and after reversing - “MADAM”, it looks similar to the input string. So this is known as palindrome.

Logic for the given problem

For the code we will create a function to do the given task. And we have a string which is the input for the function. We need to check that the given string is palindrome or not. So first we will convert the string in lowercase and remove all the other characters from the string to get the correct output. Then we will traverse the string with the help of the reverse method of Javascript and then check that the string looks similar to the input string. If these strings look similar then we will return the result as true otherwise false. So this function indicates that the string is a palindrome.

Algorithm

Step 1 − Declare a function called isPalindrome which is using an argument of string.

Step 2 − Convert the given string in lower case to get the required and actual result. And remove the extra characters and space from the string.

Step 3 − Reverse the string with the help of reverse function.

Step 4 − And check both the strings are similar.

Step 5 − Return the result as true or false.

Code for the algorithm

function isPalindrome(str) {
   // convert the string to lowercase and remove non-alphanumeric characters
   str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
    
   // reverse the string and compare with the original
   return str === str.split('').reverse().join('');
}
const str = "Level";
const str1 = "Hello Javascript";
const str2 = "Wow";
console.log(isPalindrome(str))
console.log(isPalindrome(str1))
 
//other way to show the output
const result = isPalindrome(str2);
 
if(result == true){
   console.log(str2, "is palindrome");
} else {
   console.log(str2, "is not Palindrome");
}

Complexity

The time taken by the function is O(n) because the method uses a constant time to work for every character in the given string. And n is the size of the string. And the space used by the code is O(1) as it is showing the result in boolean form.

Conclusion

So the above created function can be used to find out if the given string is palindrome or not with the time complexity O(n). We have basically used some built-in methods to solve the given problem.

Updated on: 18-May-2023

101 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements