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.
You are given a positive integer n.
For every integer x from 1 to n, we write down the integer obtained by removing all zeros from the decimal representation of x.
Return an integer denoting the number of distinct integers written down.
Example 1:
Input: n = 10
Output: 9
Explanation:
The integers we wrote down are 1, 2, 3, 4, 5, 6, 7, 8, 9, 1. There are 9 distinct integers (1, 2, 3, 4, 5, 6, 7, 8, 9).
Example 2:
Input: n = 3
Output: 3
Explanation:
The integers we wrote down are 1, 2, 3. There are 3 distinct integers (1, 2, 3).
Constraints:
1 <= n <= 1015Problem summary: You are given a positive integer n. For every integer x from 1 to n, we write down the integer obtained by removing all zeros from the decimal representation of x. Return an integer denoting the number of distinct integers written down.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming
10
3
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3747: Count Distinct Integers After Removing Zeros
class Solution {
private char[] s;
private Long[][][][] f;
public long countDistinct(long n) {
s = String.valueOf(n).toCharArray();
f = new Long[s.length][2][2][2];
return dfs(0, 0, 1, 1);
}
private long dfs(int i, int zero, int lead, int limit) {
if (i == s.length) {
return (zero == 0 && lead == 0) ? 1 : 0;
}
if (limit == 0 && f[i][zero][lead][limit] != null) {
return f[i][zero][lead][limit];
}
int up = limit == 1 ? s[i] - '0' : 9;
long ans = 0;
for (int d = 0; d <= up; d++) {
int nxtZero = zero == 1 || (d == 0 && lead == 0) ? 1 : 0;
int nxtLead = (lead == 1 && d == 0) ? 1 : 0;
int nxtLimit = (limit == 1 && d == up) ? 1 : 0;
ans += dfs(i + 1, nxtZero, nxtLead, nxtLimit);
}
if (limit == 0) {
f[i][zero][lead][limit] = ans;
}
return ans;
}
}
// Accepted solution for LeetCode #3747: Count Distinct Integers After Removing Zeros
func countDistinct(n int64) int64 {
s := []byte(fmt.Sprint(n))
m := len(s)
var f [20][2][2][2]int64
for i := range f {
for j := range f[i] {
for k := range f[i][j] {
for t := range f[i][j][k] {
f[i][j][k][t] = -1
}
}
}
}
var dfs func(i, zero, lead, limit int) int64
dfs = func(i, zero, lead, limit int) int64 {
if i == m {
if zero == 0 && lead == 0 {
return 1
}
return 0
}
if limit == 0 && f[i][zero][lead][limit] != -1 {
return f[i][zero][lead][limit]
}
up := 9
if limit == 1 {
up = int(s[i] - '0')
}
var ans int64 = 0
for d := 0; d <= up; d++ {
nxtZero := zero
if d == 0 && lead == 0 {
nxtZero = 1
}
nxtLead := 0
if lead == 1 && d == 0 {
nxtLead = 1
}
nxtLimit := 0
if limit == 1 && d == up {
nxtLimit = 1
}
ans += dfs(i+1, nxtZero, nxtLead, nxtLimit)
}
if limit == 0 {
f[i][zero][lead][limit] = ans
}
return ans
}
return dfs(0, 0, 1, 1)
}
# Accepted solution for LeetCode #3747: Count Distinct Integers After Removing Zeros
class Solution:
def countDistinct(self, n: int) -> int:
@cache
def dfs(i: int, zero: bool, lead: bool, lim: bool) -> int:
if i >= len(s):
return 1 if (not zero and not lead) else 0
up = int(s[i]) if lim else 9
ans = 0
for j in range(up + 1):
nxt_zero = zero or (j == 0 and not lead)
nxt_lead = lead and j == 0
nxt_lim = lim and j == up
ans += dfs(i + 1, nxt_zero, nxt_lead, nxt_lim)
return ans
s = str(n)
return dfs(0, False, True, True)
// Accepted solution for LeetCode #3747: Count Distinct Integers After Removing Zeros
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3747: Count Distinct Integers After Removing Zeros
// class Solution {
// private char[] s;
// private Long[][][][] f;
//
// public long countDistinct(long n) {
// s = String.valueOf(n).toCharArray();
// f = new Long[s.length][2][2][2];
// return dfs(0, 0, 1, 1);
// }
//
// private long dfs(int i, int zero, int lead, int limit) {
// if (i == s.length) {
// return (zero == 0 && lead == 0) ? 1 : 0;
// }
//
// if (limit == 0 && f[i][zero][lead][limit] != null) {
// return f[i][zero][lead][limit];
// }
//
// int up = limit == 1 ? s[i] - '0' : 9;
// long ans = 0;
// for (int d = 0; d <= up; d++) {
// int nxtZero = zero == 1 || (d == 0 && lead == 0) ? 1 : 0;
// int nxtLead = (lead == 1 && d == 0) ? 1 : 0;
// int nxtLimit = (limit == 1 && d == up) ? 1 : 0;
// ans += dfs(i + 1, nxtZero, nxtLead, nxtLimit);
// }
//
// if (limit == 0) {
// f[i][zero][lead][limit] = ans;
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3747: Count Distinct Integers After Removing Zeros
function countDistinct(n: number): number {
const s = n.toString();
const m = s.length;
const f: number[][][][] = Array.from({ length: m }, () =>
Array.from({ length: 2 }, () => Array.from({ length: 2 }, () => Array(2).fill(-1))),
);
const dfs = (i: number, zero: number, lead: number, limit: number): number => {
if (i === m) {
return zero === 0 && lead === 0 ? 1 : 0;
}
if (limit === 0 && f[i][zero][lead][limit] !== -1) {
return f[i][zero][lead][limit];
}
const up = limit === 1 ? parseInt(s[i]) : 9;
let ans = 0;
for (let d = 0; d <= up; d++) {
const nxtZero = zero === 1 || (d === 0 && lead === 0) ? 1 : 0;
const nxtLead = lead === 1 && d === 0 ? 1 : 0;
const nxtLimit = limit === 1 && d === up ? 1 : 0;
ans += dfs(i + 1, nxtZero, nxtLead, nxtLimit);
}
if (limit === 0) {
f[i][zero][lead][limit] = ans;
}
return ans;
};
return dfs(0, 0, 1, 1);
}
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.