Leetcode #2843: Count Symmetric Integers
In this guide, we solve Leetcode #2843 ** Count Symmetric Integers** in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
You are given two positive integers low and high. An integer x consisting of 2 * n digits is symmetric if the sum of the first n digits of x is equal to the sum of the last n digits of x.
Quick Facts
- Difficulty: Easy
- Premium: No
- Tags: Math, Enumeration
Intuition
There is a mathematical invariant or formula that directly leads to the result.
Using math avoids unnecessary loops and reduces complexity.
Approach
Derive the formula or update rule, then compute the answer directly.
Handle edge cases like overflow or zero carefully.
Steps:
- Identify the math relationship.
- Compute the result with a loop or formula.
- Handle edge cases.
Example
Input: low = 1, high = 100
Output: 9
Explanation: There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99.
Python Solution
class Solution:
def countSymmetricIntegers(self, low: int, high: int) -> int:
def f(x: int) -> bool:
s = str(x)
if len(s) & 1:
return False
n = len(s) // 2
return sum(map(int, s[:n])) == sum(map(int, s[n:]))
return sum(f(x) for x in range(low, high + 1))
Complexity
The time complexity is , and the space complexity is . The space complexity is .
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.