How to check whether a string is a palindrome, ignoring spaces and case in TypeScript

1 Answer

0 votes
function isPalindrome(str: string) {
  // Normalize the string: remove spaces and convert to lowercase
  const normalizedStr: string = str.replace(/\s+/g, '').toLowerCase();
  
  // Reverse the normalized string
  const reversedStr: string = normalizedStr.split('').reverse().join('');
  
  // Check if the normalized string is equal to the reversed string
  return normalizedStr === reversedStr;
}

console.log(isPalindrome("A man a plan a canal Panama")); 
console.log(isPalindrome("abcDefg")); 

 
 
/*
run:
 
true
false
 
*/

 



answered May 16, 2025 by avibootz
...