Unique 3-Digit Even Numbers (LeetCode 3483): Solution and Explanation
Given a list of digits, count the distinct three-digit even numbers you can build, using each copy of a digit at most once and never starting with 0.
Intuition
Building numbers from the digits with permutations works, but repeated digits produce the same number many times, so you would need a set to remove duplicates.
Flip the direction. There are only 450 even three-digit numbers: hundreds digit 1 to 9, tens digit 0 to 9, ones digit 0, 2, 4, 6 or 8.
For each candidate, ask one question: does the pool hold enough copies of each digit it needs? Tally the digits once, then borrow the three digits of the candidate and check that no count went negative.
Each candidate is tested exactly once, so every buildable number is counted exactly once and duplicates never appear.
Approach: Enumerate candidates against a digit count
- Count the copies of each digit into
cnt[0..9]. - Loop
afrom 1 to 9,bfrom 0 to 9 andcover 0, 2, 4, 6, 8. - Subtract one from
cnt[a],cnt[b]andcnt[c]. - If all three counts are still at least 0, the number is buildable, so add 1 to
total. - Add the three digits back and continue. Return
total.
Step-by-step example
digits = [1,2,3,4]
- cnt holds one each of 1, 2, 3 and 4.
- Hundreds digit 1: the buildable ones are 124, 132, 134 and 142. total = 4.
- Hundreds digit 2: only 214 and 234 work, because the 2 is already used up as the hundreds digit. total = 6.
- Hundreds digit 3 gives 312, 314, 324, 342 and hundreds digit 4 gives 412 and 432. total = 12.
- Hundreds digits 5 to 9 are not in the pool, so they add nothing. Return 12.
Code: Python, Java, C++
1class Solution:
2 def totalNumbers(self, digits: List[int]) -> int:
3 cnt = [0] * 10
4 for d in digits:
5 cnt[d] += 1
6 total = 0
7 for a in range(1, 10):
8 for b in range(10):
9 for c in range(0, 10, 2):
10 cnt[a] -= 1
11 cnt[b] -= 1
12 cnt[c] -= 1
13 if cnt[a] >= 0 and cnt[b] >= 0 and cnt[c] >= 0:
14 total += 1
15 cnt[a] += 1
16 cnt[b] += 1
17 cnt[c] += 1
18 return total1class Solution {
2 public int totalNumbers(int[] digits) {
3 int[] cnt = new int[10];
4 for (int d : digits) {
5 cnt[d]++;
6 }
7 int total = 0;
8 for (int a = 1; a < 10; a++) {
9 for (int b = 0; b < 10; b++) {
10 for (int c = 0; c < 10; c += 2) {
11 cnt[a]--;
12 cnt[b]--;
13 cnt[c]--;
14 if (cnt[a] >= 0 && cnt[b] >= 0 && cnt[c] >= 0) {
15 total++;
16 }
17 cnt[a]++;
18 cnt[b]++;
19 cnt[c]++;
20 }
21 }
22 }
23 return total;
24 }
25}1class Solution {
2public:
3 int totalNumbers(vector<int>& digits) {
4 int cnt[10] = {0};
5 for (int d : digits) {
6 cnt[d]++;
7 }
8 int total = 0;
9 for (int a = 1; a < 10; a++) {
10 for (int b = 0; b < 10; b++) {
11 for (int c = 0; c < 10; c += 2) {
12 cnt[a]--;
13 cnt[b]--;
14 cnt[c]--;
15 if (cnt[a] >= 0 && cnt[b] >= 0 && cnt[c] >= 0) {
16 total++;
17 }
18 cnt[a]++;
19 cnt[b]++;
20 cnt[c]++;
21 }
22 }
23 }
24 return total;
25 }
26};Time and space complexity
O(n)
Counting is O(n) and the candidate loop is a fixed 450 checks.
O(1)
Only a 10-slot count array is used.
Other approaches
Permutations into a set (time O(n^3), space O(n^3))
Generate every ordered choice of 3 positions, keep the even ones without a leading zero, and put them in a set. With at most 10 digits that is 720 triples, which is fine, but it needs the set to remove duplicates.
Choose distinct digit values (time O(n), space O(1))
Loop over digit values instead of positions and check counts, exactly as the main approach does, but skip hundreds digits with a zero count early. It is the same idea with a small speedup.
Common mistakes
- Counting permutations by position. With [0,2,2] the two 2s give the same numbers twice, but the answer is 2, not 4.
- Allowing a leading zero. Start the hundreds digit at 1.
- Forgetting that a digit used in the hundreds place is no longer available for the tens or ones place. Borrowing from the count handles this automatically.
- Forgetting to put the borrowed digits back before the next candidate.
Frequently asked questions
Why is testing all 450 candidates fast enough?
450 is a constant. Each check touches three counts, so the whole loop does a few thousand operations no matter what the input is.
How does this avoid counting duplicates?
Each number from 100 to 998 is considered once. It is either buildable or not, so it adds at most 1 to the total.
What does borrowing from the count check, exactly?
If a candidate needs a digit twice, like 202, subtracting twice from cnt[2] goes negative unless the pool holds two 2s. That catches every case where a digit is reused too often.