Editorial
Solution walkthrough
Review the explanation, then compare the reference implementation.
Video explanation
Solution
Intuition
There are only 450 three-digit even numbers, so testing candidate numbers is simpler than permuting digit positions and removing duplicates. A candidate is possible exactly when its digit frequencies do not exceed the available frequencies.
Approach
Count the supplied digits. Enumerate three-digit values in ascending order, retaining only even candidates, and count each candidate's three digits. Append it when every required count is available. Python filters evenness while scanning all three-digit values; JavaScript enumerates only even values. Starting at 100 excludes leading-zero results, and enumerating values rather than index triples automatically produces sorted, distinct output while still respecting repeated-digit availability.
class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
map = defaultdict(int)
for x in digits: map[x] += 1
res = []
for i in range(100, 1000):
a = i % 10
b = (i // 10) % 10
c = (i // 100) % 10
temp = defaultdict(int)
temp[a] += 1
temp[b] += 1
temp[c] += 1
if temp[a] <= map[a] and temp[b] <= map[b] and temp[c] <= map[c] and i % 2 == 0:
res.append(i)
return resComplexity analysis
Let d = digits.length. Python tests the 900 three-digit values and JavaScript directly tests the 450 even values, so both take O(d + 900) = O(d) time under the fixed decimal domain. Auxiliary space is O(1) apart from the bounded result array.
