Strictly Palindromic Number (LeetCode 2396): Solution and Explanation

Medium
Math
Two Pointers
Brainteaser

Decide whether n reads as a palindrome in every base from 2 to n - 2. It looks like a conversion problem, but a single base breaks every input, so the answer is always false.

Intuition

The brute-force plan is to convert n to each base from 2 to n - 2 and check each digit string with two pointers. That works, and for most n it fails within the first couple of bases.

To prove it fails for every n, find one base whose representation is predictable. Base n - 2 is ideal: n is one copy of n - 2 plus a remainder of 2, so it is written with the two digits 1 and 2.

"12" is not a palindrome, so every n of at least 5 fails in base n - 2. The only smaller input allowed is 4, whose single base 2 gives 100, also not a palindrome. The function can return false without looking at n.

Approach: Proof by one base

  1. For n >= 5, base n - 2 is at least 3, so the digit 2 is valid in it.
  2. n = 1 * (n - 2) + 2, so n is "12" in base n - 2, which is not a palindrome.
  3. For n = 4, base 2 gives "100", also not a palindrome.
  4. Return false.

Step-by-step example

n = 9
  1. Base 2: 9 = 1001. Front and back digits match and so do the middle two, so it is a palindrome.
  2. Base 3: 9 = 100. The front digit 1 does not match the back digit 0, so 9 is not strictly palindromic.
  3. The general argument: in base 7 (that is, n - 2), 9 = 1 * 7 + 2, written 12. That fails for every n.
  4. Return false.

Code: Python, Java, C++

1class Solution:
2    def isStrictlyPalindromic(self, n: int) -> bool:
3        # In base n - 2, n = 1 * (n - 2) + 2 is written "12": never a palindrome.
4        return False
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Proof by one base O(1)
Brute force over every base O(n log n)
Time

O(1)

The function returns a constant without any computation.

Space

O(1)

No memory is used.

Other approaches

Brute force over every base (time O(n log n), space O(log n))

Convert n to each base b from 2 to n - 2 and test the digits with two pointers, returning false at the first failure. It gives the same answer and is a good way to discover the pattern, but in the worst case it does O(n log n) work.

Common mistakes

  • Checking bases up to n - 1 or n. Base n - 1 gives "11", a palindrome, which is why the problem stops at n - 2.
  • Building the base-b string with characters for digits above 9. Comparing digit lists avoids the need for letters.
  • Assuming the brute force must be fast enough without thinking about why it returns early. The proof shows it always does.

Frequently asked questions

Why does the problem stop at base n - 2?

In base n - 1, n is written "11", which is a palindrome for every n. Including that base would not change the answer, but stopping at n - 2 leaves exactly the base that makes the answer false.

Is returning false a trick that LeetCode accepts?

Yes. It is the intended solution once you prove it. Any correct brute force returns false for every allowed n as well.

What about n = 4, where n - 2 = 2?

In base 2 the digit 2 is not allowed, so the "12" argument does not apply. Instead 4 is 100 in base 2, which is not a palindrome.