String Palindrome Check

Program to check if a string is palindrome

JavaScriptBeginner
JavaScript
// 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"));

Output

madam: true
hello: false
A man a plan: true

Two-pointer:
racecar: true
world: false

Recursive:
level: true
hello: false

Using every:
deed: true

This program demonstrates different methods to check if a string is a palindrome.

Palindrome Definition

Reads same forwards and backwards:

  • "madam" → "madam" ✅
  • "hello" → "olleh" ❌

Method 1: Compare with Reversed

Simple approach:

javascript
str === str.split('').reverse().join('');

String Cleaning:

  • toLowerCase(): Case-insensitive
  • replace(/[^a-z0-9]/g, ''): Remove non-alphanumeric

Method 2: Two-Pointer

Most efficient:

javascript
let left = 0;
let right = str.length - 1;
while (left < right) {
    if (str[left] !== str[right]) return false;
    left++;
    right--;
}
return true;

Pros:

  • O(n) time, O(1) space
  • No string reversal needed
  • Most efficient

Method 3: Recursive

Elegant but less efficient:

javascript
if (str[0] !== str[str.length - 1]) return false;
return isPalindrome(str.slice(1, -1));

Method 4: Every Method

Functional approach:

javascript
str.split('').every((char, i) => {
    return char === str[str.length - 1 - i];
});

Every Method:

  • Tests if all elements pass test
  • Returns false on first failure

Time Complexity:

  • All methods: O(n)
  • Two-pointer: Best space efficiency

When to Use:

  • Reversed comparison: Simplest

  • Two-pointer: Most efficient

  • Recursive: Learning recursion

  • Every: Functional style