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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Given an integer n, return the number of positive integers in the range [1, n] that have at least one repeated digit.
Example 1:
Input: n = 20 Output: 1 Explanation: The only positive number (<= 20) with at least 1 repeated digit is 11.
Example 2:
Input: n = 100 Output: 10 Explanation: The positive numbers (<= 100) with atleast 1 repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99, and 100.
Example 3:
Input: n = 1000 Output: 262
Constraints:
1 <= n <= 109Problem summary: Given an integer n, return the number of positive integers in the range [1, n] that have at least one repeated digit.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming
20
100
1000
count-the-number-of-powerful-integers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1012: Numbers With Repeated Digits
class Solution {
private char[] s;
private Integer[][] f;
public int numDupDigitsAtMostN(int n) {
s = String.valueOf(n).toCharArray();
f = new Integer[s.length][1 << 10];
return n - dfs(0, 0, true, true);
}
private int dfs(int i, int mask, boolean lead, boolean limit) {
if (i >= s.length) {
return lead ? 0 : 1;
}
if (!lead && !limit && f[i][mask] != null) {
return f[i][mask];
}
int up = limit ? s[i] - '0' : 9;
int ans = 0;
for (int j = 0; j <= up; ++j) {
if (lead && j == 0) {
ans += dfs(i + 1, mask, true, false);
} else if ((mask >> j & 1) == 0) {
ans += dfs(i + 1, mask | 1 << j, false, limit && j == up);
}
}
if (!lead && !limit) {
f[i][mask] = ans;
}
return ans;
}
}
// Accepted solution for LeetCode #1012: Numbers With Repeated Digits
func numDupDigitsAtMostN(n int) int {
s := []byte(strconv.Itoa(n))
m := len(s)
f := make([][]int, m)
for i := range f {
f[i] = make([]int, 1<<10)
for j := range f[i] {
f[i][j] = -1
}
}
var dfs func(i, mask int, lead, limit bool) int
dfs = func(i, mask int, lead, limit bool) int {
if i >= m {
if lead {
return 0
}
return 1
}
if !lead && !limit && f[i][mask] != -1 {
return f[i][mask]
}
up := 9
if limit {
up = int(s[i] - '0')
}
ans := 0
for j := 0; j <= up; j++ {
if lead && j == 0 {
ans += dfs(i+1, mask, true, limit && j == up)
} else if mask>>j&1 == 0 {
ans += dfs(i+1, mask|(1<<j), false, limit && j == up)
}
}
if !lead && !limit {
f[i][mask] = ans
}
return ans
}
return n - dfs(0, 0, true, true)
}
# Accepted solution for LeetCode #1012: Numbers With Repeated Digits
class Solution:
def numDupDigitsAtMostN(self, n: int) -> int:
@cache
def dfs(i: int, mask: int, lead: bool, limit: bool) -> int:
if i >= len(s):
return lead ^ 1
up = int(s[i]) if limit else 9
ans = 0
for j in range(up + 1):
if lead and j == 0:
ans += dfs(i + 1, mask, True, False)
elif mask >> j & 1 ^ 1:
ans += dfs(i + 1, mask | 1 << j, False, limit and j == up)
return ans
s = str(n)
return n - dfs(0, 0, True, True)
// Accepted solution for LeetCode #1012: Numbers With Repeated Digits
/**
* [1012] Numbers With Repeated Digits
*
* Given an integer n, return the number of positive integers in the range [1, n] that have at least one repeated digit.
*
* Example 1:
*
* Input: n = 20
* Output: 1
* Explanation: The only positive number (<= 20) with at least 1 repeated digit is 11.
*
* Example 2:
*
* Input: n = 100
* Output: 10
* Explanation: The positive numbers (<= 100) with atleast 1 repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99, and 100.
*
* Example 3:
*
* Input: n = 1000
* Output: 262
*
*
* Constraints:
*
* 1 <= n <= 10^9
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/numbers-with-repeated-digits/
// discuss: https://leetcode.com/problems/numbers-with-repeated-digits/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn num_dup_digits_at_most_n(n: i32) -> i32 {
n - Self::num_not_dup_digits_at_most_n(n)
}
fn num_not_dup_digits_at_most_n(n: i32) -> i32 {
let mut n = n;
let mut digits = Vec::new();
while n > 0 {
digits.push(n % 10);
n /= 10;
}
let k = digits.len();
let mut used: [i32; 10] = [0; 10];
let mut total = 0;
for i in 1..k {
total += 9 * Self::permutation(9, i as i32 - 1);
}
for i in 0..k {
let i = k - 1 - i;
let num = digits[i];
for j in (if i == k - 1 { 1 } else { 0 })..num {
if used[j as usize] != 0 {
continue;
}
total += Self::permutation((10 - k + i) as i32, i as i32);
}
used[num as usize] += 1;
if used[num as usize] > 1 {
break;
}
if i == 0 {
total += 1;
}
}
total
}
fn permutation(n: i32, k: i32) -> i32 {
Self::factorial(n) / Self::factorial(n - k)
}
fn factorial(n: i32) -> i32 {
match n {
0 | 1 => 1,
n @ _ => n * Self::factorial(n - 1),
}
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1012_example_1() {
let n = 20;
let result = 1;
assert_eq!(Solution::num_dup_digits_at_most_n(n), result);
}
#[test]
fn test_1012_example_2() {
let n = 100;
let result = 10;
assert_eq!(Solution::num_dup_digits_at_most_n(n), result);
}
#[test]
fn test_1012_example_3() {
let n = 1000;
let result = 262;
assert_eq!(Solution::num_dup_digits_at_most_n(n), result);
}
}
// Accepted solution for LeetCode #1012: Numbers With Repeated Digits
function numDupDigitsAtMostN(n: number): number {
const s = n.toString();
const m = s.length;
const f = Array.from({ length: m }, () => Array(1 << 10).fill(-1));
const dfs = (i: number, mask: number, lead: boolean, limit: boolean): number => {
if (i >= m) {
return lead ? 0 : 1;
}
if (!lead && !limit && f[i][mask] !== -1) {
return f[i][mask];
}
const up = limit ? parseInt(s[i]) : 9;
let ans = 0;
for (let j = 0; j <= up; j++) {
if (lead && j === 0) {
ans += dfs(i + 1, mask, true, limit && j === up);
} else if (((mask >> j) & 1) === 0) {
ans += dfs(i + 1, mask | (1 << j), false, limit && j === up);
}
}
if (!lead && !limit) {
f[i][mask] = ans;
}
return ans;
};
return n - dfs(0, 0, true, 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.