# Solving Leetcode Interviews in Seconds with AI: Numbers With Repeated Digits


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "1012" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# 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 <= 109  

	# Explanation
	Here's the breakdown of the solution:

*   **Calculate Numbers with Unique Digits:** Instead of directly counting numbers with repeated digits, we calculate the count of numbers with *unique* digits within the range [1, n]. Then, we subtract this count from `n` to get the desired result.
*   **Digit-by-Digit Construction:** We decompose `n` into its digits and construct numbers with unique digits from left to right, considering the constraints imposed by the digits of `n`.  We use combinatorics to efficiently calculate the number of possibilities for each digit.
*   **Handling Leading Zeros:** We need to be careful not to include numbers with leading zeros in our count of numbers with unique digits.

*   **Runtime Complexity:** O(log n), where n is the input integer. **Storage Complexity:** O(log n)

	
	# Code
	```python
	def num_with_repeated_digits(n: int) -> int:
    """
    Given an integer n, return the number of positive integers in the range [1, n]
    that have at least one repeated digit.

    For example:
    num_with_repeated_digits(20) == 1
    num_with_repeated_digits(100) == 10
    num_with_repeated_digits(1000) == 262
    """

    def count_unique_digits(num: int) -> int:
        s = str(num)
        length = len(s)
        ans = 0

        # Count numbers with length < len(s)
        for i in range(1, length):
            ans += 9 * perm(9, i - 1)

        # Count numbers with length == len(s)
        seen = set()
        for i in range(length):
            digit = int(s[i])
            for j in range(0 if i > 0 else 1, digit):
                if j not in seen:
                    ans += perm(10 - i - 1, length - i - 1)

            if digit in seen:
                break
            seen.add(digit)
        else:  # No repeated digits, include n
            ans += 1

        return ans

    def perm(n: int, k: int) -> int:
        res = 1
        for i in range(k):
            res *= (n - i)
        return res

    return n - count_unique_digits(n)
	```
			
