Determine if a string is a palindrome

Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 3 years ago

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.

[00:00] Here we have a requirement to determine if the string is a palindrome. We will start by creating our isPalindrome function. It takes the string and returns a Boolean which is true if the string is a palindrome, or false if it isn't.

[00:19] To check if the string is a palindrome, we need to check it against its reverse. To get the reverse of the string, you split the string into an array of characters, reverse this array, and finally join back this reversed array into a single string. Now, we simply check if the reversed value is equal to the original value, and return it.

[00:43] If they are equal, then the string would be a palindrome. A more complex algorithmic challenge commonly presented after isPalindrome is to write a function to check if any permutation of the given string is a palindrome. For example, the word "civic" is a palindrome.

[01:04] Therefore, any permutation of the same characters, for example, "vicic" would also be a palindrome. Similarly, since "toot" is a palindrome, so is "toto." A non-example would be the characters of the word "civil." We start off by creating the isanyPermutationPalindrome function, which will take a string and return a Boolean.

[01:30] A simple implementation would be to simply check all the permutations of the string, and if any of those are a palindrome, we would return true. However, in this naïve implementation, the runtime complexity will be driven by the permutations function, which is of the order and pictorial wherein it's the number of characters in the string.

[01:55] We can do better. The insight to a better solution is to realize that a pattern exists among the characters of any palindrome string. Civic is a palindrome, as it has C on both sides, followed by an I, and an unmatched middle V.

[02:16] The same pattern would hold for any set of characters that can form a palindrome. All characters must be paired off except one, which is allowed to be left unpaired. The characters of civil cannot form a palindrome, as both V and L would be unmatched.

[02:37] This reduces our requirement to a simple character pairing problem. We can try character pairing using a simple string set. We split the string into its characters. For each character, if it is in the current unmatched set, then great, we can delete it as it is not matched.

[03:00] If it isn't already there, then we simply add it. Finally, after we have gone through all the characters, we simply check the count of the entries. The characters of the string can form a palindrome if the count of unpaired characters is zero or one.

[03:19] This implementation simply loops through the characters of the string and does a lenient amount of work in each iteration. Therefore, its runtime is of the order N, where N is the number of characters in the string.

Vishwas Chouhan
Vishwas Chouhan
~ 7 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
~ 4 years 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

Markdown supported.
Become a member to join the discussionEnroll Today