I looped from low to high and, for each number, converted it to a string to split the digits easily. If the length was even, I compared the sum of the first half with the second half. If they matched, I counted it as a symmetric integer. I returned the total count at the end.
class Solution:
def countSymmetricIntegers(self, low: int, high: int) -> int:
count = 0
for num in range(low, high + 1):
s = str(num)
if len(s) % 2 == 0:
mid = len(s) // 2
left_sum = sum(int(d) for d in s[:mid])
right_sum = sum(int(d) for d in s[mid:])
if left_sum == right_sum:
count += 1
return count
- Time: O(n * d), where n = high - low + 1 and d is the number of digits (max 5 for constraints)
- Space: O(1)
