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
Digit Dynamic Programming
·4 mins
Blog Algorithm Digit DP
Introduction to Digit Dynamic Programming
Algorithm
Links to algorithm-related articles
Interview
Links to interview-related articles