Talentd
FleetCode

2843. Count Symmetric Integers

Easy82.5% acceptance5 companies

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:

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:

Input: low = 1200, high = 1230
Output: 4
Explanation: There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230.

Constraints:

  • 1 <= low <= high <= 104

Hints open one at a time

Hint 1
Iterate over all numbers from low to high
Hint 2
Convert each number to a string and compare the sum of the first half with that of the second.
18:58
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def countSymmetricIntegers(self, low: int, high: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
1
100
Input
1200
1230
Your code is saved on this device.Solution