1134. Codes From Digit Tiles
A toy shop sells number tiles, each showing one digit. The array digits lists the tiles you own. Values may repeat, and each physical tile can be used at most once per code.
A code is a three-digit number from 100 to 999 (no leading zero) made by placing three different tiles side by side. Return how many different codes are divisible by 3 and can be built from your tiles. Two codes are different when the numbers differ, so a code that can be built from several tile choices is still counted once. Count the tiles per digit, then test all 900 candidates against that frequency table. Time is O(n + 900) and extra space is O(1).
Example 1
- Input:
- digits = [1,2,3]
- Output:
- 6
- Explanation:
The six arrangements 123, 132, 213, 231, 312 and 321 each have digit sum 6, so all of them are divisible by 3 and the answer is 6.
Example 2
- Input:
- digits = [3,3,3,0]
- Output:
- 3
- Explanation:
The buildable codes are 303, 330 and 333, and all three are divisible by 3, so the answer is 3.
Example 3
- Input:
- digits = [5,7]
- Output:
- 0
- Explanation:
Only two tiles are available, so no three-digit code can be built and the answer is 0.
Constraints
1 ≤ digits.length ≤ 100
0 ≤ digits[i] ≤ 9
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n + 900)
- Space
- O(1)