# LeetCode: Super Palindromes Solution

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**2 * @param {string} left3 * @param {string} right4 * @return {number}5 */6var superpalindromesInRange = function (left, right) {7  ;[left, right] = [+left, +right]8
9  const isPalindrome = s => s === s.split("").reverse().join("")10
11  let palindromes = [1, 2, 3, 4, 5, 6, 7, 8, 9]12  // as range is max of 1e18 => find from sqrt(1e18) = 1e913  // build palindrome from concatnate: xx, x0x, x1x,...x9x14  // so x must be in range 9/2 = 415  for (let i = 1; i < 10000; i++) {16    const s1 = String(i) + String(i).split("").reverse().join("")17    palindromes.push(s1)18    for (let j = 0; j < 10; j++) {19      const s2 = String(i) + String(j) + String(i).split("").reverse().join("")20      palindromes.push(s2)21    }22  }23
24  palindromes = palindromes.map(Number).sort((a, b) => a - b)25
26  let numberOfSuperPalindromesInRange = 027
28  for (const palindrome of palindromes) {29    const s = String(BigInt(palindrome) ** 2n)30    if (isPalindrome(s) && left <= Number(s) && Number(s) <= right) {31      numberOfSuperPalindromesInRange++32    }33  }34
35  return numberOfSuperPalindromesInRange36}```

leetcode

math

string

array

## Next Post

LeetCode: Maximum Distance Between A Pair Of Values

May 9, 2021

Search Posts