LeetCode 2119 - A Number After a Double Reversal
The problem asks us to determine whether a number remains unchanged after performing two digit reversals. A digit reversal means reading the digits of a number from right to left.
Difficulty: 🟢 Easy
Topics: Math
Solution
Problem Understanding
The problem asks us to determine whether a number remains unchanged after performing two digit reversals.
A digit reversal means reading the digits of a number from right to left. However, any leading zeros that appear after reversing are removed automatically because integers do not store leading zeros.
For example:
- Reversing
526gives625 - Reversing
1800gives81, because the reversed form would be0081, and the leading zeros are discarded - Reversing
0still gives0
The process described in the problem is:
- Reverse the original number
num - Reverse the result again
- Check whether the final value equals the original number
The input is a single integer num, and the output is a boolean:
- Return
trueif the double reversal produces the original number - Return
falseotherwise
The constraints are small:
0 <= num <= 10^6
This tells us that performance is not a major concern, and even straightforward solutions would work comfortably. However, the problem is really testing mathematical observation rather than implementation complexity.
The most important edge case involves trailing zeros. Any number ending in one or more zeros loses those zeros during the first reversal. Once they disappear, the second reversal cannot restore them.
For example:
1200- First reversal →
21 - Second reversal →
12 - Final result differs from original
Another important edge case is 0. Although 0 technically ends with a zero, reversing it still produces 0, so it remains unchanged after two reversals.
The problem guarantees that the input is non-negative, so we do not need to handle negative numbers.
Approaches
Brute Force Approach
The most direct solution is to literally simulate the process described in the problem.
We can write a helper function that reverses a number digit by digit:
- Extract the last digit using modulo
- Append it to a new reversed number
- Remove the last digit using integer division
- Repeat until the number becomes zero
After implementing the reversal function:
- Reverse
numonce - Reverse the result again
- Compare the final value with the original number
This approach is correct because it exactly follows the problem statement.
Although this solution is already efficient for the given constraints, it performs unnecessary computation because the problem has a simple mathematical property that lets us avoid actual reversals entirely.
Optimal Observation
The key insight is that only numbers with trailing zeros fail after a double reversal.
Why does this happen?
When reversing a number, trailing zeros become leading zeros. Since integers do not preserve leading zeros, they disappear permanently.
For example:
3400- Reverse once →
43 - Reverse again →
34
The original zeros are lost forever.
On the other hand, if a number does not end in zero, every digit is preserved during reversal, and reversing twice restores the original number.
The only exception is 0 itself:
- Reverse
0→0 - Reverse again →
0
So the condition becomes extremely simple:
- Return
trueifnum == 0 - Otherwise return whether
num % 10 != 0
Approach Comparison
| Approach | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Brute Force | O(d) | O(1) | Explicitly performs two reversals, where d is the number of digits |
| Optimal | O(1) | O(1) | Uses the trailing-zero observation |
Algorithm Walkthrough
- Check whether the number is zero.
This special case matters because 0 reversed is still 0. Even though it ends with a zero digit, it should return true.
2. If the number is not zero, check its last digit using num % 10.
The modulo operation gives the final digit of the number.
3. If the last digit is zero, return false.
A trailing zero disappears during the first reversal, so the second reversal cannot reconstruct the original number.
4. Otherwise, return true.
Numbers without trailing zeros preserve all digits through both reversals.
Why it works
A double reversal restores the original order of digits only if no information is lost during the first reversal. The only information that can disappear is leading zeros created from original trailing zeros. Therefore, every non-zero number ending with zero fails, while all other numbers succeed.
Python Solution
class Solution:
def isSameAfterReversals(self, num: int) -> bool:
return num == 0 or num % 10 != 0
The implementation directly encodes the mathematical observation.
The expression num == 0 handles the special case where the input itself is zero.
The condition num % 10 != 0 checks whether the number ends with a non-zero digit. If the last digit is non-zero, reversing twice preserves the number exactly.
Because the solution avoids constructing reversed numbers entirely, the implementation is extremely concise and efficient.
Go Solution
func isSameAfterReversals(num int) bool {
return num == 0 || num%10 != 0
}
The Go implementation is nearly identical to the Python version.
Go uses || for logical OR instead of Python's or. Integer modulo works the same way, so checking num%10 != 0 correctly determines whether the number has a trailing zero.
Since the input range is very small, there are no concerns about integer overflow.
Worked Examples
Example 1
Input:
num = 526
Step-by-step evaluation:
| Step | Value |
|---|---|
| Original number | 526 |
| Is num == 0? | No |
| Compute num % 10 | 6 |
| Is last digit zero? | No |
| Return value | true |
Explanation:
The number does not end with zero, so double reversal restores the original number.
Example 2
Input:
num = 1800
Step-by-step evaluation:
| Step | Value |
|---|---|
| Original number | 1800 |
| Is num == 0? | No |
| Compute num % 10 | 0 |
| Is last digit zero? | Yes |
| Return value | false |
Explanation:
The trailing zeros disappear after the first reversal:
1800→8181→18
The final result differs from the original number.
Example 3
Input:
num = 0
Step-by-step evaluation:
| Step | Value |
|---|---|
| Original number | 0 |
| Is num == 0? | Yes |
| Return value | true |
Explanation:
Reversing zero still produces zero, so the number remains unchanged after two reversals.
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(1) | Only a few arithmetic operations are performed |
| Space | O(1) | No extra memory proportional to input size is used |
The algorithm performs a constant number of operations regardless of the number of digits in the input. Since no loops or additional data structures are required, both time and space complexity remain constant.
Test Cases
solution = Solution()
assert solution.isSameAfterReversals(526) == True # normal number without trailing zero
assert solution.isSameAfterReversals(1800) == False # multiple trailing zeros
assert solution.isSameAfterReversals(0) == True # special zero case
assert solution.isSameAfterReversals(1) == True # single digit non-zero
assert solution.isSameAfterReversals(10) == False # smallest number with trailing zero
assert solution.isSameAfterReversals(100) == False # multiple trailing zeros
assert solution.isSameAfterReversals(101) == True # zero inside number, not trailing
assert solution.isSameAfterReversals(909) == True # internal zero preserved
assert solution.isSameAfterReversals(1203) == True # non-zero ending
assert solution.isSameAfterReversals(999999) == True # large value without trailing zero
assert solution.isSameAfterReversals(1000000) == False # upper bound with trailing zeros
| Test | Why |
|---|---|
526 |
Standard successful case |
1800 |
Trailing zeros disappear after reversal |
0 |
Special edge case |
1 |
Smallest non-zero input |
10 |
Simplest failing case |
100 |
Multiple trailing zeros |
101 |
Internal zero should not matter |
909 |
Confirms middle zeros are preserved |
1203 |
Mixed digits with non-zero ending |
999999 |
Large valid number |
1000000 |
Upper constraint boundary with trailing zeros |
Edge Cases
One important edge case is the number 0. A naive implementation based purely on checking whether the number ends in zero might incorrectly return false for 0. However, reversing zero still produces zero, so the correct answer is true. The implementation explicitly handles this case with num == 0.
Another critical edge case is numbers with trailing zeros, such as 10, 100, or 1800. These numbers lose information during the first reversal because leading zeros are discarded. The implementation correctly detects this situation using num % 10 == 0.
A third important edge case is numbers containing zeros internally but not at the end, such as 101 or 909. Internal zeros remain part of the number after reversal, so double reversal restores the original value correctly. Since the algorithm only checks for trailing zeros, these cases are handled properly.