Numbers With Repeated Digits
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int numDupDigitsAtMostN(int n) {\n        // Convert n + 1 to a character array to handle it digit by digit\n        String numStr = Integer.toString(n + 1);\n        int len = numStr.length();\n        \n        // Step 1: Count numbers without repeated digits less than the length of numStr\n        int countNoRepeat = 0;\n        for (int i = 1; i < len; i++) {\n            countNoRepeat += 9 * A(9, i - 1);\n        }\n        \n        // Step 2: Count numbers without repeated digits with exactly \'len\' digits\n        boolean[] used = new boolean[10];\n        for (int i = 0; i < len; i++) {\n            int digit = numStr.charAt(i) - \'0\';\n            for (int j = i == 0 ? 1 : 0; j < digit; j++) {\n                if (!used[j]) {\n                    countNoRepeat += A(9 - i, len - i - 1);\n                }\n            }\n            if (used[digit]) break;\n            used[digit] = true;\n        }\n        \n        // Step 3: The result is n - count of numbers without repeated digits\n        return n - countNoRepeat;\n    }\n\n    // Helper function: P(m, n) = m * (m-1) * ... * (m-n+1)\n    private int A(int m, int n) {\n        return n == 0 ? 1 : A(m, n - 1) * (m - n + 1);\n    }\n\n    public static void main(String[] args) {\n        Solution solution = new Solution();\n        System.out.println(solution.numDupDigitsAtMostN(20));   // Output: 1\n        System.out.println(solution.numDupDigitsAtMostN(100));  // Output: 10\n        System.out.println(solution.numDupDigitsAtMostN(1000)); // Output: 262\n    }\n}\n\n```
['Java']