LeetCode Problem

How to Solve Super Palindromes

Super Palindromes are numbers that are palindromes themselves and also the square of a palindrome. The key is to efficiently generate palindrome roots and check if their squares remain palindromic within the range. Handling large numbers as strings ensures correctness without integer overflow while preserving math-string enumeration efficiency.

GhostInterview Help

Need help with Super Palindromes without spending extra time grinding it?

GhostInterview can read Super Palindromes from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #906Math plus StringReviewed 2026-03-08
Difficulty
Hard
Primary pattern
Math plus String
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

Super Palindromes are numbers that are palindromes themselves and also the square of a palindrome. The key is to efficiently generate palindrome roots and check if their squares remain palindromic within the range. Handling large numbers as strings ensures correctness without integer overflow while preserving math-string enumeration efficiency.

Problem Statement

A super-palindrome is a positive integer that is itself a palindrome and also the square of a palindrome. For example, 121 is a super-palindrome because 11 squared equals 121, and both 11 and 121 read the same forwards and backwards.

Given two strings left and right representing positive integers, return the count of super-palindromes within the inclusive range [left, right]. Constraints include string lengths up to 18 digits, no leading zeros, and left not exceeding right. Example: left = "4", right = "1000" should return 4.

Examples

Example 1

Input: left = "4", right = "1000"

Output: 4

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

Example details omitted.

Constraints

  • 1 <= left.length, right.length <= 18
  • left 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.

Solution Approach

Generate Palindromic Roots

Iterate through potential palindrome roots up to the fourth root of the upper limit. Construct both odd and even length palindromes as strings to prevent numeric overflow, then square them to see if they fall within the range.

Check Squared Palindromes

For each palindromic root, compute the square and convert it to a string. Verify whether this squared number is itself a palindrome. Only count numbers that satisfy both the root and square palindrome condition.

Range Filtering and Optimization

Stop generating roots once their squares exceed the upper bound. Skip any roots whose squares are below the lower bound. Use string comparisons to handle very large numbers safely and efficiently.

Complexity Analysis

MetricValue
TimeO(W^{\frac{1}{4}} * \log W)
SpaceO(\log W)

Time complexity is O(W^{\frac{1}{4}} * \log W) due to iterating palindromic roots up to the fourth root of the upper limit, with logarithmic cost to check each square palindrome. Space complexity is O(\log W) to store temporary string representations of roots and squares.

What Interviewers Usually Probe

  • Focus on combining string manipulation with math to generate valid palindromic roots.
  • Check for both odd and even length palindrome squares to avoid missing edge cases.
  • Handle very large numeric ranges using string-based comparisons to prevent integer overflow.

Common Pitfalls or Variants

Common pitfalls

  • Assuming all squares of palindrome roots are automatically palindromes.
  • Overlooking the need to generate both odd and even length palindromes.
  • Using integer types that cannot accommodate numbers up to 10^{18}.

Follow-up variants

  • Count super-palindromes within a range but only considering odd-length palindromes.
  • Return the actual list of super-palindromes instead of just the count.
  • Modify the problem to find super-palindromes that are cubes of palindromes instead of squares.

How GhostInterview Helps

  • GhostInterview generates palindromic roots efficiently and filters squares within the range instantly.
  • It highlights edge cases like even vs. odd length palindromes to avoid undercounting.
  • The tool validates string-based math operations for very large numbers safely.

Topic Pages

FAQ

What is a super-palindrome in this context?

A super-palindrome is a number that is a palindrome itself and also the square of a palindrome number.

Why do we generate palindrome roots as strings?

Using strings prevents integer overflow and allows efficient palindrome construction for both odd and even lengths.

Can GhostInterview handle ranges up to 10^{18}?

Yes, it manages large numeric ranges safely with string-based comparisons and root generation up to the fourth root.

Do we need to check both odd and even length roots?

Yes, some super-palindromes come from odd-length roots while others from even-length roots, missing one type undercounts results.

What pattern does this problem mainly test?

This problem primarily tests the 'Math plus String' pattern, combining numerical calculations with string palindrome checks.

GhostInterview Solver

Need direct help with Super Palindromes instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Super Palindromes from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.