Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created April 11, 2025 22:45
Show Gist options
  • Save Ifihan/3e5e732104487f3740348659bad08543 to your computer and use it in GitHub Desktop.
Save Ifihan/3e5e732104487f3740348659bad08543 to your computer and use it in GitHub Desktop.
Count Symmetric Integers

Question

Approach

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.

Implementation

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

Complexities

  • Time: O(n * d), where n = high - low + 1 and d is the number of digits (max 5 for constraints)
  • Space: O(1)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment