<p>Let&#39;s say a positive integer is a <strong>super-palindrome</strong> if it is a palindrome, and it is also the square of a palindrome.</p>

<p>Given two positive integers <code>left</code> and <code>right</code> represented as strings, return <em>the number of <strong>super-palindromes</strong> integers in the inclusive range</em> <code>[left, right]</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> left = &quot;4&quot;, right = &quot;1000&quot;
<strong>Output:</strong> 4
<strong>Explanation</strong>: 4, 9, 121, and 484 are superpalindromes.
Note that 676 is not a superpalindrome: 26 * 26 = 676, but 26 is not a palindrome.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> left = &quot;1&quot;, right = &quot;2&quot;
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= left.length, right.length &lt;= 18</code></li>
	<li><code>left</code> and <code>right</code> consist of only digits.</li>
	<li><code>left</code> and <code>right</code> cannot have leading zeros.</li>
	<li><code>left</code> and <code>right</code> represent integers in the range <code>[1, 10<sup>18</sup> - 1]</code>.</li>
	<li><code>left</code> is less than or equal to <code>right</code>.</li>
</ul>

class Solution {
public:
    bool IsPalinDrome(long num) {
      string s = to_string(num);
      int i = 0, j = s.size() - 1;
      while (i <= j) {
        if (s[i] != s[j]) return false;
        ++i, --j;
      }
      return true;
    }

    vector<string> GetPalindromes(int n) {
      vector<string> res;
      if (n % 2 == 0) {
        res = {""};
      } else {
        res = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",};
      }
      for (int i = 0; i < n / 2; ++i) {
        vector<string> tmp = res;
        res.clear();
        for (const string& s : tmp) {
          for (char c = '0'; c <= '9'; ++c) {
            if (s.size() == n - 2 && c == '0') continue;
            string new_s;
            new_s.push_back(c);
            new_s += s;
            new_s.push_back(c);
            res.push_back(new_s);
          }
        }
      }
      return res;
    }
    
    int superpalindromesInRange(string left, string right) {
      int count = 0;
      long left_num = sqrt(stol(left));
      long  right_num = sqrt(stol(right));
      int min_size = to_string(left_num).size();
      int max_size = to_string(right_num).size();
      for (int size = min_size; size <= max_size; ++size) {
        vector<string> v = GetPalindromes(size);
        for (string& s : v) {
        long num = stol(s);
        if (num < left_num || num > right_num) continue;
        if (IsPalinDrome(num * num)) {
          count++;
        }
        }
      }
      return count;
    }
};
160400