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.
Input: str = "xox"
Output: true
Input: str = "aman"
Output: false
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
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
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
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
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
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
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
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
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
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