Digit DP
Code\n\nclass Solution {\npublic:\n    int dp[10][2][1025][10];\n    int solve(int index, int tight, int mask, int zeroCount, string &s) {\n        if(index == 
# Code\n```\nclass Solution {\npublic:\n    int dp[10][2][1025][10];\n    int solve(int index, int tight, int mask, int zeroCount, string &s) {\n        if(index == s.length()) {\n            return 1;\n        }\n        if(dp[index][tight][mask][zeroCount] != -1) {\n            return dp[index][tight][mask][zeroCount];\n        }\n        int ub = tight ? s[index] - \'0\' : 9;\n        int ans = 0;\n        for(int i = 0; i <= ub; i++) {\n            int newZeroCount = zeroCount;\n            if(i == 0) {\n                if(mask > 0) {\n                    newZeroCount++;\n                } else {\n                    if(newZeroCount >= 1) newZeroCount++;\n                }\n                if(newZeroCount <= 1) ans = ans + solve(index + 1, (tight && i == ub), mask, newZeroCount, s);\n            } else {\n                if((mask & (1 << i)) == 0) {\n                    ans = ans + solve(index + 1, (tight && i == ub), (mask | (1 << i)), zeroCount, s);\n                }\n            }\n        }\n        return dp[index][tight][mask][zeroCount] = ans;\n    }\n    int numDupDigitsAtMostN(int n) {\n        memset(dp, -1 ,sizeof(dp));\n        string s = to_string(n);\n        int ans = n - (solve(0, 1, 0, 0, s) - 1);\n        return ans;\n\n    }\n};\n  \n```
['C++']