The Palindrome Number Problem: Strings vs. Maths in JavaScript

Hero image for The Palindrome Number Problem: Strings vs. Maths in JavaScript. Image by Jakob Owens.
Hero image for 'The Palindrome Number Problem: Strings vs. Maths in JavaScript.' Image by Jakob Owens.

In the world of algorithms, palindromes frequently pop up as intriguing structures to investigate. A palindrome is something that reads the same backwards as it does forwards, and while they are often associated with words like radar or racecar, they can apply to numbers too (like 121).

If you want to read the original problem first, you can find Palindrome Number on LeetCode. But how do we determine if a number is a palindrome? The answer lies in two main approaches: string manipulation and mathematical operations.


Understanding the Problem

Given an integer x, determine whether it is a palindrome. An integer is a palindrome when it reads the same backwards as forwards. For instance, 121 is a palindrome, whilst 121 is not (it reads 121 backwards).

The catch here is that some solutions, like converting the integer to a string to check for a palindrome, might seem straightforward, but may not be particularly optimal.

Constraints

  • Do not convert the integer into a string to solve the problem (although, for the sake of exploration, I will consider this method too).
  • Consider negative numbers as nonpalindromic. 121 is not the same as 121.

This problem serves as an excellent exercise in understanding number manipulation, string operations, and the nuances between different algorithmic solutions. As we delve deeper, I'll describe both stringbased and mathematical methods, evaluating the pros and cons of each.


Setting the Scene

When we see a number (like 1234321), it is fairly evident to our human eyes that it reads the same backwards. However, for a computer, it's not as straightforward.

There are two main approaches that can be considered in solving this:

  • Convert the number to a string and check for equality.
  • Use mathematical operations to reverse the number and compare.

The String Method

Personally, this is the method I reached for first. It is quite intuitive to convert the number to a string and then check if it is equal when read forwards and backwards.

For example:

const isPalindromeString = (num: number): boolean => {  const str = num.toString();  return str === str.split('').reverse().join('');};

This method is simple, but comes with the overhead of string operations, and is unlikely to be efficient for very large numbers.


The Mathematical Method

Instead of relying on string manipulations, we can reverse the number mathematically and then check if the original and reversed numbers are the same. Like this:

const isPalindromeMath = (num: number): boolean => {  if (num < 0 || (num % 10 === 0 && num !== 0)) return false;  let reversed = 0;  while (num > reversed) {    reversed = reversed * 10 + (num % 10);    num = Math.floor(num / 10);  }  return num === reversed || num === Math.floor(reversed / 10);};

If you have read other articles on my blog, you might recognise that this is very similar to another integer problem: reversing numbers coincidentally also with a stringbased and a mathsbased solution.

How It Works

  • Negative numbers are not palindromic by definition in this context.
  • We reverse half of the number to avoid overflow issues.
  • If the length of the number is odd, the middle digit doesn't matter in palindromicity, so we floordivide the reversed number by 10.

Why Maths Over String?

Although the stringbased solution is straightforward, the mathematical solution offers several advantages:

  1. Performance:

    String operations, especially reverse, can be costly for very long strings. Directly manipulating numbers might be faster.
  2. Memory Efficiency:

    The string method can be memoryintensive due to the creation of new string instances.
  3. Versatility:

    The mathematical solution works in environments that may not support string operations conveniently although this seems unlikely in JavaScript!

Final Thoughts

When it comes to solving the Palindrome Number Problem, both stringbased and mathematicsbased methods achieve the same goal. However, in programming as in life there's often more than one way to skin a cat. The string method might suffice for smallscale applications or for quickly testing an idea. However, for optimised solutions, especially in environments where performance matters, the mathematical approach shines.

By understanding these underlying mechanisms, web developers can make informed decisions, optimising not just for solution correctness but also for efficiency and scalability.


Categories:

  1. Algorithms
  2. Development
  3. Guides
  4. JavaScript
  5. LeetCode