Super Palindromes - Problem

A super-palindrome is a positive integer that satisfies two conditions:

  • It is a palindrome (reads the same forwards and backwards)
  • It is the square of a palindrome

Given two positive integers left and right represented as strings, return the number of super-palindromes in the inclusive range [left, right].

Example: If we have a palindrome like 11, its square is 121, which is also a palindrome. Therefore, 121 is a super-palindrome.

Input & Output

Example 1 — Small Range
$ Input: left = "1", right = "10"
Output: 2
💡 Note: Super-palindromes in [1,10]: 4 (2² and both 4,2 are palindromes) and 9 (3² and both 9,3 are palindromes)
Example 2 — Larger Range
$ Input: left = "1", right = "1000"
Output: 4
💡 Note: Super-palindromes: 1 (1²), 4 (2²), 9 (3²), and 121 (11²). All are palindromes and squares of palindromes.
Example 3 — High Range
$ Input: left = "100", right = "200"
Output: 1
💡 Note: Only 121 (11²) is a super-palindrome in range [100,200]. 121 is palindrome and 11 is palindrome.

Constraints

  • 1 ≤ int(left) ≤ int(right) ≤ 1018
  • left and right consist of only digits
  • left and right will not have leading zeros

Visualization

Tap to expand
Super Palindromes: Range [1, 25]PalindromesSquaresSuper-Palindromes123111491211² = 12² = 43² = 911² = 121149✓ palindrome✓ palindrome✓ palindrome✗ out of rangeCount = 3 super-palindromes in range [1, 25](1, 4, 9 are palindromes AND squares of palindromes)
Understanding the Visualization
1
Input
Range [left, right] to search for super-palindromes
2
Process
Find numbers that are palindromes AND squares of palindromes
3
Output
Count of super-palindromes in the range
Key Takeaway
🎯 Key Insight: Generate palindromes first, then check if their squares are also palindromes - much more efficient than checking every number!
Asked in
Google 15 Facebook 8
18.5K Views
Medium Frequency
~45 min Avg. Time
432 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen