2998 Count Symmetric Integers
2998 Count Symmetric Integers
Count Symmetric Integers 
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. Numbers with an odd number of digits are never symmetric.
Return the number of symmetric integers in the range [low, high].
Example 1:
1
2
3
4
5
**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.
Example 2:
1
2
3
4
5
**Input:** low = 1200, high = 1230
**Output:** 4
**Explanation:** There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230.
Constraints:
1
1 <= low <= high <= 104
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
def countSymmetricIntegers(self, low: int, high: int) -> int:
res = 0
for k in range(low, high + 1):
m = str(k)
if len(m) % 2 != 0:
continue
a = m[0:len(m)//2]
b = m[len(m)//2:]
s1, s2 = 0,0
for p in a:
s1 += int(p)
for p in b:
s2 += int(p)
if s1 == s2:
res += 1
return res
This post is licensed under CC BY 4.0 by the author.