<p>Given a <strong>non-negative</strong> integer <code>num</code>, return <code>true</code><em> if </em><code>num</code><em> can be expressed as the sum of any <strong>non-negative</strong> integer and its reverse, or </em><code>false</code><em> otherwise.</em></p>

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

<pre>
<strong>Input:</strong> num = 443
<strong>Output:</strong> true
<strong>Explanation:</strong> 172 + 271 = 443 so we return true.
</pre>

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

<pre>
<strong>Input:</strong> num = 63
<strong>Output:</strong> false
<strong>Explanation:</strong> 63 cannot be expressed as the sum of a non-negative integer and its reverse so we return false.
</pre>

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

<pre>
<strong>Input:</strong> num = 181
<strong>Output:</strong> true
<strong>Explanation:</strong> 140 + 041 = 181 so we return true. Note that when a number is reversed, there may be leading zeros.
</pre>

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

<ul>
	<li><code>0 &lt;= num &lt;= 10<sup>5</sup></code></li>
</ul>

dig_pairs = [(x, y) for x in range(10) for y in range(max(x - 1, 0), 10)]
dig_map = {
    n: [(x, y) for (x, y) in dig_pairs if (x + y) % 10 == n]
    for n in range(0, 10)
}

class Solution:
    def sumOfNumberAndReverse(self, num: int) -> bool:
        if not num:
            return True

        def are_valid(digits: List[Tuple[int]]) -> bool:
            n = len(digits)
            return not any(
                digits[n - 1 - i][0] != digits[i][1]
                or digits[n - 1 - i][1] != digits[i][0]
                for i in range(n)
            )

        def backtrack(n: int, used_digits: List[Tuple[int]]) -> bool:
            if n < 0:
                return False
            if n == 0:
                return are_valid(used_digits)
            dig = n % 10
            for x, y in dig_map[dig]:
                used_digits.append((x, y))
                if backtrack(n // 10 - (x + y) // 10, used_digits):
                    return True
                used_digits.pop()

            return False

        return backtrack(num, [])
121