Difficulty: Easy
Given that it is an easy question and the highest number possible is 10^4
, we can brute force the solution.
We just iterate through all the possible integers from low
to high
and we skip all integers that have odd number of digits. If the number of digits is even, we sum up the first n/2
digits and the last n/2
digits. If the sums are equal to each other, we add 1 to the count. We return the count at the end.
class Solution(object):
def countSymmetricIntegers(self, low, high):
"""
:type low: int
:type high: int
:rtype: int
"""
count = 0
for i in range(low, high + 1):
if len(str(i)) % 2 == 1:
continue
n = len(str(i))
first = sum([int(x) for x in list(str(i)[:n / 2])])
second = sum([int(x) for x in list(str(i)[n / 2:])])
if first == second:
count += 1
return count
Time Complexity:
O(high - low)
Space Complexity:
O(1)
Time Taken:
forgot to keep track