String Palindrome Check
Program to check if a string is palindrome
JavaScript String Palindrome Check Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Method 1: Compare with reversed string
function isPalindrome1(str) {
str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
return str === str.split('').reverse().join('');
}
console.log("madam:", isPalindrome1("madam"));
console.log("hello:", isPalindrome1("hello"));
console.log("A man a plan:", isPalindrome1("A man a plan a canal Panama"));
// Method 2: Two-pointer approach
function isPalindrome2(str) {
str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
let left = 0;
let right = str.length - 1;
while (left < right) {
if (str[left] !== str[right]) {
return false;
}
left++;
right--;
}
return true;
}
console.log("\nTwo-pointer:");
console.log("racecar:", isPalindrome2("racecar"));
console.log("world:", isPalindrome2("world"));
// Method 3: Recursive approach
function isPalindrome3(str) {
str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
if (str.length <= 1) return true;
if (str[0] !== str[str.length - 1]) return false;
return isPalindrome3(str.slice(1, -1));
}
console.log("\nRecursive:");
console.log("level:", isPalindrome3("level"));
console.log("hello:", isPalindrome3("hello"));
// Method 4: Using every method
function isPalindrome4(str) {
str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
return str.split('').every((char, i) => {
return char === str[str.length - 1 - i];
});
}
console.log("\nUsing every:");
console.log("deed:", isPalindrome4("deed"));madam: true hello: false A man a plan: true Two-pointer: racecar: true world: false Recursive: level: true hello: false Using every: deed: true
Understanding String Palindrome Check
This program demonstrates different methods to check if a string is a palindrome.
Palindrome Definition
Reads same forwards and backwards:
Method 1: Compare with Reversed
Simple approach:
str === str.split('').reverse().join('');
String Cleaning:
toLowerCase(): Case-insensitivereplace(/[^a-z0-9]/g, ''): Remove non-alphanumericMethod 2: Two-Pointer
Most efficient:
let left = 0;
let right = str.length - 1;
while (left < right) {
if (str[left] !== str[right]) return false;
left++;
right--;
}
return true;Pros:
Method 3: Recursive
Elegant but less efficient:
if (str[0] !== str[str.length - 1]) return false;
return isPalindrome(str.slice(1, -1));Method 4: Every Method
Functional approach:
str.split('').every((char, i) => {
});
return char === str[str.length - 1 - i];Every Method:
Time Complexity:
When to Use:
-
Reversed comparison
: Simplest
-
Two-pointer
: Most efficient
-
Recursive
: Learning recursion
-
Every
: Functional style
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.