How to Solve Valid Palindrome Problem with 1 Line of JavaScript

How to Solve Valid Palindrome Problem with 1 Line of JavaScript

ยท

3 min read

Unless you're just learning how to code now, you've probably heard of the famous valid palindrome or 'is palindrome' problem. It's a common technical interview question.

I'm going to show you how to knock the socks off your interviewer by solving the palindrome problem in 1 line of JavaScript. ๐Ÿ˜Ž

javascript-1-line-surprised.png

Quick Refresher: What is the Palindrome Problem?

A string is a palindrome, if after reversing the order of all the characters, you get the same string. const str = 'abba' is a palindrome, const str = 'abc' is not.

Here's the challenge: Given a valid string of letters, can you construct a function that will check if the string is a valid palindrome. It returns true if it is, and false if it's not.

IsPalindrome(...) in 1 Line

const isPalindrome = (str, first = 0, last = str.length - 1) => 
  (first > last) ? 
    true : 
    (str[first] === str[last]) && isPalindrome(str, first + 1, last - 1);

Yes, this is 1 line of JavaScript code. I added a few line breaks and tabs to make it easier to read.

If the above function looks overwhelming to you, no worries, we're going to walkthrough all the JavaScript and Recursion Jui Jitsu together. ๐Ÿ˜…

Let's start with...

Arrow Function

I'm using a JavaScript arrow function so I can remove return and taken advantage of implicit return feature the arrow function offers. Love arrow functions! ๐Ÿ˜

const isPalindrome = (str, first = 0, last = str.length - 1) =>

Default values for function arguments

We've initialized our 2 pointers right in the function argument: first = 0 and last = str.length - 1. Super cool and convenient.

Ternary operator

  (first > last) ? 
    true : 
    (str[first] === str[last]) && isPalindrome(str, first + 1, last - 1);

The ternary conditional operator is just a if {...} else {...} statement condensed onto 1 line. Here's a simple example:

(x > 0) ? 
console.log('x is greater than zero') : 
console.log('x is less than or equal to zero');

Finally Everyone's Favorite: Recursion!

(str[first] === str[last]) && isPalindrome(str, first + 1, last - 1);

Fortunately, this is a simple application of recursion that won't give anyone a headache trying to comprehend it. ๐Ÿคฏ

In the above statement: (str[first] === str[last]) checks to make sure the first and last characters of the string are matching. Then isPalindrome(str, first + 1, last - 1) recursively checks if the 2nd and 2nd to last characters of the string are matching.

For example: If we ran str = abcba through isPalindrome(str), and expanded out the final return statement (after running through each iteration of recursion) it would look like this:

(str[0] === str[4]) && (str[1] === str[3]) && (str[2] === str[2]) && true

isPalindrome(str) is checking each pair of elements starting with str[0] versus str[4] (a vs a), until finally it's checking str[2] against itself when first === last.

The true statement at the end is tacked on when first > last, completing the last recursive function call.

Wait! What if I have to deal with spaces, uppercase, and weird characters??

OK, yea, in some Palindrome problems, such as this leetcode version you have strings with space, special characters and different cases that you have to remove or somehow deal with.

Fear not, there's a 1 line fix to cleaning up the string so it contains only lowercase letters. Run this before running isPalindrome(str):

str = str.match(/[a-z]/gi).join('').toLowerCase();

or we can keep isPalindrome to 1 line, like I promised:

const isPalindrome = (str = str.match(/[a-z]/gi).join('').toLowerCase(), first = 0, last = str.length - 1) => 
  (first > last) ? 
    true : 
    (str[first] === str[last]) && isPalindrome(str, first + 1, last - 1);

cat palindrome

Did you find this article valuable?

Support Amit Mehta by becoming a sponsor. Any amount is appreciated!

ย