Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
Given an integer n, return the count of all numbers with unique digits, x, where 0 <= x < 10n.
Example 1:
Input: n = 2 Output: 91 Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100, excluding 11,22,33,44,55,66,77,88,99
Example 2:
Input: n = 0 Output: 1
Constraints:
0 <= n <= 8Problem summary: Given an integer n, return the count of all numbers with unique digits, x, where 0 <= x < 10n.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming · Backtracking
2
0
count-special-integers)count-numbers-with-unique-digits-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #357: Count Numbers with Unique Digits
class Solution {
private Integer[][] f;
public int countNumbersWithUniqueDigits(int n) {
f = new Integer[n][1 << 10];
return dfs(n - 1, 0, true);
}
private int dfs(int i, int mask, boolean lead) {
if (i < 0) {
return 1;
}
if (!lead && f[i][mask] != null) {
return f[i][mask];
}
int ans = 0;
for (int j = 0; j <= 9; ++j) {
if ((mask >> j & 1) == 1) {
continue;
}
if (lead && j == 0) {
ans += dfs(i - 1, mask, true);
} else {
ans += dfs(i - 1, mask | 1 << j, false);
}
}
if (!lead) {
f[i][mask] = ans;
}
return ans;
}
}
// Accepted solution for LeetCode #357: Count Numbers with Unique Digits
func countNumbersWithUniqueDigits(n int) int {
f := make([][1 << 10]int, n)
for i := range f {
for j := range f[i] {
f[i][j] = -1
}
}
var dfs func(i, mask int, lead bool) int
dfs = func(i, mask int, lead bool) int {
if i < 0 {
return 1
}
if !lead && f[i][mask] != -1 {
return f[i][mask]
}
ans := 0
for j := 0; j < 10; j++ {
if mask>>j&1 == 1 {
continue
}
if lead && j == 0 {
ans += dfs(i-1, mask, true)
} else {
ans += dfs(i-1, mask|1<<j, false)
}
}
if !lead {
f[i][mask] = ans
}
return ans
}
return dfs(n-1, 0, true)
}
# Accepted solution for LeetCode #357: Count Numbers with Unique Digits
class Solution:
def countNumbersWithUniqueDigits(self, n: int) -> int:
@cache
def dfs(i: int, mask: int, lead: bool) -> int:
if i < 0:
return 1
ans = 0
for j in range(10):
if mask >> j & 1:
continue
if lead and j == 0:
ans += dfs(i - 1, mask, True)
else:
ans += dfs(i - 1, mask | 1 << j, False)
return ans
return dfs(n - 1, 0, True)
// Accepted solution for LeetCode #357: Count Numbers with Unique Digits
struct Solution;
impl Solution {
fn count_numbers_with_unique_digits(n: i32) -> i32 {
if n < 11 {
Self::f(n)
} else {
Self::f(10)
}
}
fn f(n: i32) -> i32 {
match n {
1 => 10,
2 => 9 * 9 + 10,
3 => 9 * 9 * 8 + 9 * 9 + 10,
4 => 9 * 9 * 8 * 7 + 9 * 9 * 8 + 9 * 9 + 10,
5 => 9 * 9 * 8 * 7 * 6 + 9 * 9 * 8 * 7 + 9 * 9 * 8 + 9 * 9 + 10,
6 => 9 * 9 * 8 * 7 * 6 * 5 + 9 * 9 * 8 * 7 * 6 + 9 * 9 * 8 * 7 + 9 * 9 * 8 + 9 * 9 + 10,
7 => {
9 * 9 * 8 * 7 * 6 * 5 * 4
+ 9 * 9 * 8 * 7 * 6 * 5
+ 9 * 9 * 8 * 7 * 6
+ 9 * 9 * 8 * 7
+ 9 * 9 * 8
+ 9 * 9
+ 10
}
8 => {
9 * 9 * 8 * 7 * 6 * 5 * 4 * 3
+ 9 * 9 * 8 * 7 * 6 * 5 * 4
+ 9 * 9 * 8 * 7 * 6 * 5
+ 9 * 9 * 8 * 7 * 6
+ 9 * 9 * 8 * 7
+ 9 * 9 * 8
+ 9 * 9
+ 10
}
9 => {
9 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
+ 9 * 9 * 8 * 7 * 6 * 5 * 4 * 3
+ 9 * 9 * 8 * 7 * 6 * 5 * 4
+ 9 * 9 * 8 * 7 * 6 * 5
+ 9 * 9 * 8 * 7 * 6
+ 9 * 9 * 8 * 7
+ 9 * 9 * 8
+ 9 * 9
+ 10
}
10 => {
9 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
+ 9 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
+ 9 * 9 * 8 * 7 * 6 * 5 * 4 * 3
+ 9 * 9 * 8 * 7 * 6 * 5 * 4
+ 9 * 9 * 8 * 7 * 6 * 5
+ 9 * 9 * 8 * 7 * 6
+ 9 * 9 * 8 * 7
+ 9 * 9 * 8
+ 9 * 9
+ 10
}
0 => 1,
_ => 0,
}
}
}
#[test]
fn test() {
let n = 2;
let res = 91;
assert_eq!(Solution::count_numbers_with_unique_digits(n), res);
}
// Accepted solution for LeetCode #357: Count Numbers with Unique Digits
function countNumbersWithUniqueDigits(n: number): number {
const f: number[][] = Array.from({ length: n }, () => Array(1 << 10).fill(-1));
const dfs = (i: number, mask: number, lead: boolean): number => {
if (i < 0) {
return 1;
}
if (!lead && f[i][mask] !== -1) {
return f[i][mask];
}
let ans = 0;
for (let j = 0; j < 10; ++j) {
if ((mask >> j) & 1) {
continue;
}
if (lead && j === 0) {
ans += dfs(i - 1, mask, true);
} else {
ans += dfs(i - 1, mask | (1 << j), false);
}
}
if (!lead) {
f[i][mask] = ans;
}
return ans;
};
return dfs(n - 1, 0, true);
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.