I iterated through all 3-digit numbers from 100 to 999, ensuring they are even (i.e., last digit is even). For each candidate, I checked if it could be formed using the available digits considering their counts. I used Counter to handle duplicates correctly, ensuring I only used digits as many times as they appear. I stored valid numbers in a set to avoid duplicates and finally returned them sorted.
class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
result = set()
counter = Counter(digits)
for i in range(100, 1000):
d1, d2, d3 = i // 100, (i // 10) % 10, i % 10
if d3 % 2 == 1:
continue
candidate_counter = Counter([d1, d2, d3])
if all(candidate_counter[d] <= counter[d] for d in candidate_counter):
result.add(i)
return sorted(result)
- Time: O(1)
- Space: O(1)
