Finding 3-Digit Even Numbers — LeetCode 2094 Python Solution

EasyRecursionArrayHash TableEnumerationSorting
Problem
#2094
Pattern
Sorting
Reading time
2 min

The problem

You are given an integer array digits, where each element is a digit. The array may contain duplicates.

Example

Input
digits = [2,1,3,0]
Output
[102,120,130,132,210,230,302,310,312,320]
Explanation
All the possible integers that follow the requirements are in the output array.

Python solution

Python
class Solution:
    def findEvenNumbers(self, digits: List[int]) -> List[int]:
        cnt = Counter(digits)
        ans = []
        for x in range(100, 1000, 2):
            cnt1 = Counter()
            y = x
            while y:
                y, v = divmod(y, 10)
                cnt1[v] += 1
            if all(cnt[i] >= cnt1[i] for i in range(10)):
                ans.append(x)
        return ans

Complexity

MeasureComplexity
TimeO(k \times 10^k), where k is the number of digits of the target even number, which is 3 in this problem
SpaceO(1) auxiliary

Pattern: Sorting

Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 2094. Finding 3-Digit Even Numbers is filed here because LeetCode tags it Sorting, which is the vocabulary this hub collects.

The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2094. Finding 3-Digit Even Numbers?
LeetCode 2094. Finding 3-Digit Even Numbers is rated Easy on LeetCode.
What is the time complexity of LeetCode 2094. Finding 3-Digit Even Numbers?
The Python solution on this page runs in O(k \times 10^k), where k is the number of digits of the target even number, which is 3 in this problem.
What is the space complexity of LeetCode 2094. Finding 3-Digit Even Numbers?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2094. Finding 3-Digit Even Numbers cover?
LeetCode 2094. Finding 3-Digit Even Numbers is tagged Recursion, Array, Hash Table, Enumeration and Sorting 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