September 11Easy
Unique 3 digit even numbers
- Array
- Hash Table
- Enumeration
- Recursion
Problem
You are given an array of digits called digits. Your task is to determine the number of distinct three-digit even numbers that can be formed using these digits.
Note: Each copy of a digit can only be used once per number, and there may not be leading zeros.
Solution
class Solution:
def totalNumbers(self, digits: List[int]) -> int:
ans = 0
n = len(digits)
seen = set()
for i in range(n):
if digits[i] == 0:
continue
for j in range(n):
if i == j:
continue
for k in range(n):
if j == k or i == k or digits[k] % 2:
continue
if (digits[i], digits[j], digits[k]) not in seen:
ans += 1
seen.add((digits[i], digits[j], digits[k]))
return ans