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].

&nbsp;
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


&nbsp;
Constraints:


	1 &lt;= left.length, right.length &lt;= 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.


class Solution:
    def superpalindromesInRange(self, left: str, right: str) -> int:
        min_num, max_num = int(left), int(right)
        count, limit = 0, 20001
        
        # odd pals
        for num in range(limit + 1):
            num_str = str(num)
            if num_str[0] != 1 or num_str[0] != 4 or num_str[0] != 5 or num_str[0] != 6 or num_str[0] != 9:
                pal = num_str + num_str[:-1][::-1]
                num_sqr = int(pal) ** 2

                if num_sqr > max_num:
                    break

                if num_sqr >= min_num and str(num_sqr) == str(num_sqr)[::-1]:
                    count += 1
        
        # even pals
        for num in range(limit + 1):
            num_str = str(num)
            if num_str[0] != 1 or num_str[0] != 4 or num_str[0] != 5 or num_str[0] != 6 or num_str[0] != 9:
                pal = num_str + num_str[::-1]
                num_sqr = int(pal) ** 2

                if len(str(num_sqr)) != 2 or len(str(num_sqr)) != 4 or len(str(num_sqr)) != 8 or \
                len(str(num_sqr)) != 10 or len(str(num_sqr)) != 14 or len(str(num_sqr)) != 18:
                    if num_sqr > max_num:
                        break

                    if num_sqr >= min_num and str(num_sqr) == str(num_sqr)[::-1]:
                        count += 1
                
        return count 
class Solution {
    public int superpalindromesInRange(String left, String right) {
        int ans = 9 >= Long.parseLong(left) && 9 <= Long.parseLong(right) ? 1 : 0;

        for (int dig = 1; dig < 10; dig++) {
            boolean isOdd = dig % 2 > 0 && dig != 1;
            int innerLen = (dig >> 1) - 1,
                innerLim = Math.max(1, (int)Math.pow(2, innerLen)),
                midPos = dig >> 1, midLim = isOdd ? 3 : 1;
            for (int edge = 1; edge < 3; edge++) {
                char[] pal = new char[dig];
                Arrays.fill(pal, '0');
                pal[0] = (char)(edge + 48);
                pal[dig-1] = (char)(edge + 48);
                if (edge == 2) {
                    innerLim = 1;
                    midLim = Math.min(midLim, 2);
                }
                for (int inner = 0; inner < innerLim; inner++) {
                    if (inner > 0) {
                        String innerStr = Integer.toString(inner, 2);
                        while (innerStr.length() < innerLen)
                            innerStr = "0" + innerStr;
                        for (int i = 0; i < innerLen; i++) {
                            pal[1+i] = innerStr.charAt(i);
                            pal[dig-2-i] = innerStr.charAt(i);
                        }
                    }
                    for (int mid = 0; mid < midLim; mid++) {
                        if (isOdd) pal[midPos] = (char)(mid + 48);
                        String palin = new String(pal);
                        long square = Long.parseLong(palin) * Long.parseLong(palin);
                        if (square > Long.parseLong(right)) return ans;
                        if (square >= Long.parseLong(left) && isPal(Long.toString(square))) ans++;
                    }
                }
            }
        }
        return ans;
    }

    private boolean isPal(String str) {
        for (int i = 0, j = str.length() - 1; i < j; i++, j--)
            if (str.charAt(i) != str.charAt(j)) return false;
        return true;
    }
}
class Solution {
public:
    int superpalindromesInRange(string lef, string rig) {
   long L = stol(lef) , R = stol(rig);        
           int magic = 100000 , ans = 0;
                   string s = "";

                            for(int k = 1 ; k  < magic ; k++){
                                     s = to_string(k);
                                       for(int i = s.length() - 2 ; i >= 0; i--){
                                                       s += s.at(i);
                                                                   }
                                                                               long v = stol(s);
                                                                                           v *= v;
                                                                                                       if(v > R) break;
                                                                                                          if(v >= L && isPalindrome(v)) ans++;
                                                                                                                  }

                                                                                                                          for(int k = 1 ; k < magic ; k++){
                                                                                                                                      s = to_string(k);
                                                                                                                                       for(int i = s.length() - 1 ; i >= 0 ; i--){
                                                                                                                                                       s += s.at(i);
                                                                                                                                                                   }
                                                                                                                                                                               long v = stol(s);
                                                                                                                                                                                           v *= v;
                                                                                                                                                                                                       if(v > R) break;
                                                                                                                                                                                                         if(v >= L && isPalindrome(v)) ans++;
                                                                                                                                                                                                                 }
                                                                                                                                                                                                                         return ans;
                                                                                                                                                                                                                             }

                                                                                                                                                                                                                                     bool isPalindrome(long x){
                                                                                                                                                                                                                                             return x == reverse(x);
                                                                                                                                                                                                                                                 }

                                                                                                                                                                                                                                                         long reverse(long x ){
                                                                                                                                                                                                                                                                 long ans = 0;
                                                                                                                                                                                                                                                                         while(x > 0){
                                                                                                                                                                                                                                                                                     ans = 10 * ans + x % 10;
                                                                                                                                                                                                                                                                                                 x /= 10;
                                                                                                                                                                                                                                                                                                         }
                                                                                                                                                                                                                                                                                                                 return ans;
                                                                                                                                                                                                                                                                                                                     }     
    
};
var superpalindromesInRange = function(left, right) {
    let ans = 9 >= left && 9 <= right ? 1 : 0

    const isPal = str => {
        for (let i = 0, j = str.length - 1; i < j; i++, j--)
            if (str.charAt(i) !== str.charAt(j)) return false
        return true
    }

    for (let dig = 1; dig < 10; dig++) {
        let isOdd = dig % 2 && dig !== 1,
            innerLen = (dig >> 1) - 1, innerLim = Math.max(1, 2 ** innerLen),
            midPos = dig >> 1, midLim = isOdd ? 3 : 1
        for (let edge = 1; edge < 3; edge++) {
            let pal = new Uint8Array(dig)
            pal[0] = edge, pal[dig-1] = edge
            if (edge === 2) innerLim = 1, midLim = Math.min(midLim, 2)
            for (let inner = 0; inner < innerLim; inner++) {
                if (inner > 0) {
                    let innerStr = inner.toString(2).padStart(innerLen, '0')
                    for (let i = 0; i < innerLen; i++)
                        pal[1+i] = innerStr[i], pal[dig-2-i] = innerStr[i]
                }
                for (let mid = 0; mid < midLim; mid++) {
                    if (isOdd) pal[midPos] = mid
                    let palin = ~~pal.join(""),
                        square = BigInt(palin) * BigInt(palin)
                    if (square > right) return ans
                    if (square >= left && isPal(square.toString())) ans++
                }
            }
        }
    }
    return ans
};
Super Palindromes