Check if a String is a Palindrome

medium

By - Aman Pareek

Last Updated - 02/09/2024

Problem Statement

Write a function to determine whether a given string is a palindrome. A palindrome is a string that reads the same forwards and backwards, ignoring case and spaces.

Example 1

Input: str = "xox"

Output: true

Example 2

Input: str = "aman"

Output: false

Solution 1: Using split, reverse, and join

function isPalindromeUsingSplitReverseJoin(str) {
  const cleanedStr = str.replace(/\s+/g, '').toLowerCase();
  return cleanedStr === cleanedStr.split("").reverse().join("");
} 

const str1 = "xox";
isPalindromeUsingSplitReverseJoin(str1);  //output: true 

const str2 = "aman";
isPalindromeUsingSplitReverseJoin(str2);  //output: false 

Solution 2: Using a for Loop

function isPalindromeUsingForLoop(str) {
  const cleanedStr = str.replace(/\s+/g, '').toLowerCase();
  let left = 0;
  let right = cleanedStr.length - 1;
  while (left < right) {
    if (cleanedStr[left] !== cleanedStr[right]) return false;
    left++;
    right--;
  }
  return true;
} 

const str1 = "xox";
isPalindromeUsingForLoop(str1);  //output: true 

const str2 = "aman";
isPalindromeUsingForLoop(str2);  //output: false 

Solution 3: Using a while Loop

function isPalindromeUsingWhileLoop(str) {
  const cleanedStr = str.replace(/\s+/g, '').toLowerCase();
  let start = 0;
  let end = cleanedStr.length - 1;
  while (start < end) {
    if (cleanedStr[start] !== cleanedStr[end]) return false;
    start++;
    end--;
  }
  return true;
} 

const str1 = "xox";
isPalindromeUsingWhileLoop(str1);  //output: true 

const str2 = "aman";
isPalindromeUsingWhileLoop(str2);  //output: false 

Solution 4: Using Array.prototype.every

function isPalindromeUsingEvery(str) {
  const cleanedStr = str.replace(/\s+/g, '').toLowerCase();
  return [...cleanedStr].every((char, index) => char === cleanedStr[cleanedStr.length - 1 - index]);
} 

const str1 = "xox";
isPalindromeUsingEvery(str1);  //output: true 

const str2 = "aman";
isPalindromeUsingEvery(str2);  //output: false 

Solution 5: Using Array.prototype.reduce

function isPalindromeUsingReduce(str) {
  const cleanedStr = str.replace(/\s+/g, '').toLowerCase();
  return cleanedStr.split("").reduce((isPalin, char, index) => isPalin && char === cleanedStr[cleanedStr.length - 1 - index], true);
} 

const str1 = "xox";
isPalindromeUsingReduce(str1);  //output: true 

const str2 = "aman";
isPalindromeUsingReduce(str2);  //output: false 

Solution 6: Using String.prototype.match

function isPalindromeUsingMatch(str) {
  const cleanedStr = str.replace(/\s+/g, '').toLowerCase();
  const reversedStr = cleanedStr.split("").reverse().join("");
  return reversedStr === cleanedStr;
} 

const str1 = "xox";
isPalindromeUsingMatch(str1);  //output: true 

const str2 = "aman";
isPalindromeUsingMatch(str2);  //output: false 

Solution 7: Using Recursion

function isPalindromeUsingRecursion(str) {
  const cleanedStr = str.replace(/\s+/g, '').toLowerCase();
  
  function checkPalindrome(left, right) {
    if (left >= right) return true;
    if (cleanedStr[left] !== cleanedStr[right]) return false;
    return checkPalindrome(left + 1, right - 1);
  }
  
  return checkPalindrome(0, cleanedStr.length - 1);
} 

const str1 = "xox";
isPalindromeUsingRecursion(str1);  //output: true 

const str2 = "aman";
isPalindromeUsingRecursion(str2);  //output: false 

Solution 8: Using Two-Pointer Technique

function isPalindromeUsingTwoPointer(str) {
  const cleanedStr = str.replace(/\s+/g, '').toLowerCase();
  let left = 0;
  let right = cleanedStr.length - 1;
  while (left < right) {
    if (cleanedStr[left] !== cleanedStr[right]) return false;
    left++;
    right--;
  }
  return true;
} 

const str1 = "xox";
isPalindromeUsingTwoPointer(str1);  //output: true 

const str2 = "aman";
isPalindromeUsingTwoPointer(str2);  //output: false 

Solution 9: Using String.prototype.replace

function isPalindromeUsingReplace(str) {
  const cleanedStr = str.replace(/\s+/g, '').toLowerCase();
  const reversedStr = cleanedStr.split("").reverse().join("");
  return cleanedStr === reversedStr;
} 

const str1 = "xox";
isPalindromeUsingReplace(str1);  //output: true 

const str2 = "aman";
isPalindromeUsingReplace(str2);  //output: false 

Solution 10: Using Set and Array.from

function isPalindromeUsingSet(str) {
  const cleanedStr = str.replace(/\s+/g, '').toLowerCase();
  const length = cleanedStr.length;
  return Array.from({ length }).every((_, i) => cleanedStr[i] === cleanedStr[length - 1 - i]);
} 

const str1 = "xox";
isPalindromeUsingSet(str1);  //output: true 

const str2 = "aman";
isPalindromeUsingSet(str2);  //output: false 

Popular Solutions