Sum of Number and Its Reverse — LeetCode 2443 Python Solution

MediumMathEnumeration
Problem
#2443
Reading time
2 min

The problem

Given a non-negative integer num, return true if num can be expressed as the sum of any non-negative integer and its reverse, or false otherwise.

Example

Input
num = 443
Output
true
Explanation
172 + 271 = 443 so we return true.

Python solution

Python
class Solution:
    def sumOfNumberAndReverse(self, num: int) -> bool:
        return any(k + int(str(k)[::-1]) == num for k in range(num + 1))

Complexity

MeasureComplexity
TimeO(n \times \log n), where n is the size of num
SpaceO(1) auxiliary

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 2443. Sum of Number and Its Reverse is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2443. Sum of Number and Its Reverse?
LeetCode 2443. Sum of Number and Its Reverse is rated Medium on LeetCode.
What is the time complexity of LeetCode 2443. Sum of Number and Its Reverse?
The Python solution on this page runs in O(n \times \log n), where n is the size of num.
What is the space complexity of LeetCode 2443. Sum of Number and Its Reverse?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2443. Sum of Number and Its Reverse cover?
LeetCode 2443. Sum of Number and Its Reverse is tagged Math and Enumeration on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview