Advertisement

Numbers With Repeated Digits - LeetCode 1012 Solution

Numbers With Repeated Digits - Complete Solution Guide

Numbers With Repeated Digits is LeetCode problem 1012, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem asks us to find the number of positive integers within the range [1, n] that have at least one repeated digit. For example, if n is 20, the only number with repeated digits is 11. If n is 100, the numbers with repeated digits are 11, 22, 33, 44, 55, 66, 77, 88, 99, and 100. The constraint is that 1 <= n <= 10^9.

Solution Approach

The solution counts the number of integers from 1 to n that have *no* repeated digits and subtracts this count from n to get the desired result (numbers with at least one repeated digit). It first counts the number of integers with unique digits that have length less than the length of n+1. Then, it iterates through the digits of n+1, and for each digit, it counts the number of valid numbers with unique digits that have the same prefix as n+1 up to that digit. The permutation function is used to calculate number of such combinations. Finally, the numbers of repeated digits are calculated by n - number of numbers with unique digits.

Step-by-Step Algorithm

  1. Step 1: Convert the input integer `n+1` to a string `s` to easily access individual digits.
  2. Step 2: Calculate the number of integers with unique digits that have a length smaller than the length of s. This is done by iterating from length 1 to length k-1 (where k is the length of s) and using the formula 9 * P(9, i-1). We multiply by 9 because the first digit cannot be 0.
  3. Step 3: Initialize a set `used_digits` to track digits already used in the current number being considered.
  4. Step 4: Iterate through the digits of `s` from left to right. For each digit `d` at index `i`, iterate through possible digits `j` (from 1 if i==0, or 0 otherwise) up to `d-1`. If `j` is not in `used_digits`, it means we can construct a valid number with unique digits. Then, add P(10-(i+1), k-(i+1)) to `count_unique`.
  5. Step 5: If `d` is already in `used_digits`, it means adding `d` would create a number with repeated digits, so stop.
  6. Step 6: Add the digit `d` to `used_digits` for the next iteration.
  7. Step 7: Finally, return n - count_unique, which gives the number of integers with at least one repeated digit.

Key Insights

  • Insight 1: The problem can be solved more efficiently by counting the numbers with *no* repeated digits and subtracting that from n.
  • Insight 2: The number of integers with unique digits and a given length can be calculated using permutations.
  • Insight 3: The solution uses a digit-by-digit approach to account for numbers less than or equal to n, efficiently pruning the search space.

Complexity Analysis

Time Complexity: O(log(N))

Space Complexity: O(log(N))

Topics

This problem involves: Math, Dynamic Programming.

Companies

Asked at: IBM, J.P. Morgan.