You are given an integer array digits
, where each element is a digit. The array may contain duplicates.
You need to find all the unique integers that follow the given requirements:
digits
in any arbitrary order.For example, if the given digits
were [1, 2, 3]
, integers 132
and 312
follow the requirements.
Return a sorted array of the unique integers.
Example 1:
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. Notice that there are no odd integers or integers with leading zeros.
Example 2:
Input: digits = [2,2,8,8,2] Output: [222,228,282,288,822,828,882] Explanation: The same digit can be used as many times as it appears in digits. In this example, the digit 8 is used twice each time in 288, 828, and 882.
Example 3:
Input: digits = [3,7,5] Output: [] Explanation: No even integers can be formed using the given digits.
Constraints:
3 <= digits.length <= 100
0 <= digits[i] <= 9
This problem asks us to find all unique 3-digit even numbers that can be formed using the digits provided in the input array digits
. The solution involves several steps:
1. Counting Digit Frequencies:
We first count the frequency of each digit (0-9) present in the input array digits
. This is efficiently done using a Counter
(Python) or a simple array (other languages) to store the counts.
2. Enumerating Even Numbers:
We iterate through all even numbers between 100 and 999 (inclusive). This is a simple loop incrementing by 2.
3. Checking Digit Availability:
For each even number, we check if it can be formed using the digits from the input array. We do this by:
digits
array. If the frequency of any digit in the number exceeds its frequency in digits
, we know this number cannot be formed and move to the next even number.4. Building the Result:
If all digits of the current even number are available (based on the frequency check in step 3), we add the number to our result array.
5. Sorting and Returning:
Finally, we sort the resulting array of even numbers and return it. Sorting is necessary to meet the problem's requirement of returning a sorted array.
Time Complexity: The dominant factor is the loop iterating through even numbers from 100 to 999. There are approximately 450 such numbers. For each number, we extract digits (constant time) and perform a frequency check (constant time). Therefore, the overall time complexity is O(N), where N is approximately 450 (the number of 3-digit even numbers). This is effectively constant time since N is a relatively small fixed number.
Space Complexity: The space used is primarily for storing the digit counts (cnt
array or Counter
) and the resulting array of even numbers (ans
). The size of the cnt
array is constant (10). The size of ans
is at most 450. Therefore, the space complexity is O(1) (constant space), since the space used does not grow with the input size.
Python:
from collections import Counter
class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
# Count the frequency of each digit
digit_counts = Counter(digits)
result = []
# Iterate through 3-digit even numbers
for num in range(100, 1000, 2):
num_str = str(num) # Convert to string for easy digit extraction
num_counts = Counter(num_str) # Count digit frequencies in current number
# Check if all digits are available
can_form = True
for digit in num_counts:
if num_counts[digit] > digit_counts[int(digit)]:
can_form = False
break
# Add to result if formable
if can_form:
result.append(num)
return sorted(result) # Return sorted result
The other languages (Java, C++, Go, TypeScript, JavaScript) follow a very similar structure, differing only in syntax. The core logic of counting digit frequencies, iterating through even numbers, checking availability, and building/sorting the result remains consistent across all implementations.