Digit DP and Bitmasking Solution
\n\n# Code\n\nclass Solution {\npublic:\n    int digits[10];\n    int dp[10][2][2][2][1024];\n\n    int solve(int pos, int tight, int repeated, int zero, int ma
\n\n# Code\n```\nclass Solution {\npublic:\n    int digits[10];\n    int dp[10][2][2][2][1024];\n\n    int solve(int pos, int tight, int repeated, int zero, int mask, string &s) {\n        if (pos == s.length())\n            return !zero && repeated;\n\n        if (dp[pos][tight][repeated][zero][mask] != -1)\n            return dp[pos][tight][repeated][zero][mask];\n\n        int ans = 0;\n        int limit = tight ? (s[pos] - \'0\') : 9;\n\n        for (int i = 0; i <= limit; i++) {\n            if (!zero || i > 0){\n                digits[i]++;\n                ans += solve(pos + 1, tight && (i == limit), repeated || digits[i] >= 2, zero && (i == 0), mask|(1<<i), s);\n                digits[i]--;\n            }\n            else{\n                ans += solve(pos + 1, tight && (i == limit), repeated || digits[i] >= 2, zero && (i == 0), mask, s);\n            }\n        }\n\n        return dp[pos][tight][repeated][zero][mask] = ans;\n    }\n\n    int numDupDigitsAtMostN(int n) {\n        string s = to_string(n);\n        memset(digits, 0, sizeof(digits));\n        memset(dp, -1, sizeof(dp));\n        return solve(0, 1, 0, 1, 0, s);\n    }\n};\n\n\n```
['Dynamic Programming', 'Bitmask', 'C++']