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 2 3 4
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.
class Solution: def superpalindromesInRange(self, L, R): """ :type L: str :type R: str :rtype: int """ que = collections.deque(["11", "22"]) candi = set() while que: size = len(que) for _ in range(size): p = que.popleft() candi.add(p) if int(p) ** 2 > int(R): continue for j in ["0", "1", "2"]: q = (p[:len(p)//2] + j + p[len(p)//2:]) que.append(q) candi.add("1") candi.add("2") candi.add("3") res = 0 for cand in candi: if int(L) <= int(cand) ** 2 <= int(R) and self.isPalindromes(int(cand) ** 2): res += 1 return res def isPalindromes(self, s): s = str(s) N = len(s) for l in range(1, N // 2): if s[l] != s[N - 1 - l]: return False return True