Find Numbers with Even Number of Digits — LeetCode 1295 Python Solution

EasyArrayMath
Problem
#1295
Reading time
2 min

The problem

Given an array nums of integers, return how many of them contain an even number of digits.

Example

Input
nums = [12,345,2,6,7896]
Output
2
Explanation
12 contains 2 digits (even number of digits).

Python solution

Python
class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        return sum(len(str(x)) % 2 == 0 for x in nums)

Complexity

MeasureComplexity
TimeO(n \times \log M)
SpaceO(\log M) auxiliary

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1295. Find Numbers with Even Number of Digits is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1295. Find Numbers with Even Number of Digits?
LeetCode 1295. Find Numbers with Even Number of Digits is rated Easy on LeetCode.
What is the time complexity of LeetCode 1295. Find Numbers with Even Number of Digits?
The Python solution on this page runs in O(n \times \log M).
What is the space complexity of LeetCode 1295. Find Numbers with Even Number of Digits?
The Python solution on this page uses O(\log M) auxiliary space.
What topics does LeetCode 1295. Find Numbers with Even Number of Digits cover?
LeetCode 1295. Find Numbers with Even Number of Digits is tagged Array and Math on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview