Watch 10 video solutions for Super Palindromes, a hard level problem involving Math, Enumeration. This walkthrough by Algorithms Made Easy has 2,187 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Let's say a positive integer is a super-palindrome if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers left and right represented as strings, return the number of super-palindromes integers in the inclusive range [left, right].
Example 1:
Input: left = "4", right = "1000" Output: 4 Explanation: 4, 9, 121, and 484 are superpalindromes. Note that 676 is not a superpalindrome: 26 * 26 = 676, but 26 is not a palindrome.
Example 2:
Input: left = "1", right = "2" Output: 1
Constraints:
1 <= left.length, right.length <= 18left and right consist of only digits.left and right cannot have leading zeros.left and right represent integers in the range [1, 1018 - 1].left is less than or equal to right.Problem Overview: Given two numbers L and R, count how many numbers in this range are super palindromes. A super palindrome is a number that is itself a palindrome and also the square of another palindrome. The key challenge is the huge range (up to 1018), which makes direct enumeration impossible.
Approach 1: Brute Force Generation of Palindromes (Enumeration, ~O(k * d))
Instead of checking every number between L and R, generate palindromic numbers as potential square roots. Compute sqrt(R), then enumerate palindromes up to this bound. For each generated palindrome p, compute p * p and check two conditions: the square lies within [L, R] and the square itself is also a palindrome. Palindrome checks use string comparison from both ends. If the maximum root is around 109, the number of palindromes is only about 105, making enumeration feasible. Time complexity is roughly O(k * d) where k is the number of generated palindromes and d is the digit length for palindrome checks, with O(1) extra space.
This approach relies heavily on enumeration and simple math observations about square roots. It dramatically reduces the search space compared to scanning the entire interval.
Approach 2: Iterative Palindrome Construction (Optimized Enumeration, O(k))
Construct palindromes directly by building them from a numeric prefix. Iterate over a prefix i, convert it to a string, and mirror it to produce both odd-length and even-length palindromes. Each constructed value represents a palindromic root candidate. Square the value and check whether the result lies inside [L, R] and remains a palindrome.
The insight is that every palindrome can be formed by mirroring a prefix. This avoids expensive checks over arbitrary numbers and guarantees that only valid palindrome roots are generated. Since only about 100k candidates exist below 109, the algorithm runs quickly. Time complexity is O(k) for generating and validating candidates, with O(1) auxiliary space.
This method is essentially structured enumeration combined with digit manipulation. It is the standard competitive programming solution because it prunes the search space aggressively.
Recommended for interviews: Interviewers expect the palindrome-root enumeration approach. A naive brute force over the numeric range demonstrates understanding of the problem definition but fails on constraints. Constructing palindromes iteratively and squaring them shows stronger algorithmic reasoning and familiarity with math-based pruning techniques.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Generation of Palindromes | O(k * d) | O(1) | When enumerating palindrome roots up to sqrt(R) and validating squares with simple string checks. |
| Iterative Palindrome Construction | O(k) | O(1) | Best for large ranges up to 10^18 where generating palindromes via mirrored prefixes minimizes candidates. |