↓Skip to main content
  1. LeetCode/

LeetCode 2843: Count Symmetric Integers

·1 min· ·
LeetCode Daily Easy Digit-Dp
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-04-11

  • Total time: 5:00.00

Intuition
#

I was considering using Digit DP to solve this problem, but I noticed that the problem constraint specifies the maximum number is only up to 10^5.

Therefore, we can simply use brute-force to iterate through all numbers from start to end. For each number, we skip it if it’s odd. If it’s even, we find the middle index and compare the sum of digits from the start to the middle with the sum from the middle to the end.

This approach has a time complexity of O(n) and a space complexity of O(1).

Approach
#

class Solution:
    def countSymmetricIntegers(self, low: int, high: int) -> int:
        
        res = 0
        for num in range(low, high+1):
            str_num = str(num)

            if len(str_num)%2:
                continue
            
            half_index = len(str_num)//2
            if sum(map(int, list(str_num[:half_index]))) == sum(map(int, list(str_num[half_index:]))):
                res+=1

        return res

Findings
#

N/A

Encountered Problems
#

N/A

Related

LeetCode 2999: Count the Number of Powerful Integers
·2 mins
LeetCode Daily Hard Digit-Dp
LeetCode Problem Solving
LeetCode 617: Merge Two Binary Trees
·2 mins
LeetCode Daily Easy Tree
LeetCode Problem Solving
LeetCode 3375: Minimum Operations to Make Array Values Equal to K
·2 mins
LeetCode Daily
LeetCode Problem Solving
LeetCode 3507: Minimum Pair Removal to Sort Array I
·2 mins
LeetCode Easy Simulation Array
LeetCode 3507 note: n is tiny, so simulate; the right side of a slice assignment must be a list
LeetCode 3492: Maximum Containers on a Ship
·1 min
LeetCode Easy Math
LeetCode 3492 note: at most n² cells and at most maxWeight // w by weight, so take the smaller, using integer division
LeetCode 1431: Kids With the Greatest Number of Candies
·1 min
LeetCode Easy Array
LeetCode 1431 note: compute the maximum once; a kid is True when their candies plus the extra reach >= the maximum, since a tie counts