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:
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.
Example 2:
1 2
Input: left = "1", right = "2" Output: 1
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].
classSolution { public: intsuperpalindromesInRange(string L, string R){ int res = 0; long left = stol(L), right = stol(R); helper("", left, right, res); for (char c = '0'; c <= '9'; ++c) { helper(string(1, c), left, right, res); } return res; } voidhelper(string cur, long& left, long& right, int& res){ if (cur.size() > 9) return; if (!cur.empty() && cur[0] != '0') { long num = stol(cur); num *= num; if (num > right) return; if (num >= left && isPalindrome(to_string(num))) ++res; } for (char c = '0'; c <= '9'; ++c) { helper(string(1, c) + cur + string(1, c), left, right, res); } } boolisPalindrome(string str){ int left = 0, right = (int)str.size() - 1; while (left < right) { if (str[left++] != str[right--]) returnfalse; } returntrue; } };