Determine if a string is a palindrome

Share this video with your friends

Send Tweet

A palindrome is a string that reads the same forward and backward, for example, radar, toot, and madam. In this lesson we discuss how to approach the palindrome problem using TypeScript / JavaScript.

We also discuss a more complex algorithmic challenge of writing a function to check if any permutation of a given string is a palindrome.

Vishwas Chouhan
Vishwas Chouhan
~ 4 years ago

This is pretty neat! But for the 2 solution isAnyPermutationPalindrome you're missing.

// Check 1

if(str.length <= 1) return true; 

// Check 2 No need to do anything if the first and the last char don't match. it can also include to convert the char to lowerCase.

if (str.chatAt[0] !== str.charAt(str.length - 1) return false;

then your implementation of the str.split()...

Also, the same solution can be done without forEach using the recursive strategy and using the above two conditions to exit without running into the recursive call.

function isAnyPermutationPalindrome(str) {
    if (str.length <= 1) return true;
    if (str.charAt(0) !== str.charAt(text.length - 1)) return false;
    return isPalindrome(str.substr(1, str.length - 2));
}

Lastly, 'vicic' is not palindrome as the first and the last chat don't match.

Alex Jacobs
Alex Jacobs
~ 8 months ago

Check 2 doesn't apply for the permutation version.

"vicic" isnt a palindrome, but one of its permutations is "civic" which is a palindrome