Digit DP + Bitmask
Code\n\nclass Solution {\npublic:\n\n    int dp[10][2][2][2][1024];\n\n    int okay(string s, int i, bool tight, bool lead, bool dup, int mask)\n    {\n        
# Code\n```\nclass Solution {\npublic:\n\n    int dp[10][2][2][2][1024];\n\n    int okay(string s, int i, bool tight, bool lead, bool dup, int mask)\n    {\n        if(i == s.size())\n        {\n            return dup;\n        }\n\n        if(dp[i][tight][lead][dup][mask] != -1)\n        {\n            return dp[i][tight][lead][dup][mask];\n        }\n\n        int x = (tight)? s[i]-\'0\': 9;\n        int cnt = 0;\n\n        for(int j = 0; j <= x; j++)\n        {\n            if(j!= 0 || (j == 0 && lead == 0))\n            {\n                if(((1<<j)&mask))\n                {               \n                    cnt+=okay(s,i+1,(tight&(j==x)),0,1,(mask|(1<<j)));\n                }\n                else\n                {\n                    cnt+=okay(s,i+1,(tight&(j==x)),0,dup,(mask|(1<<j)));\n                }\n            }\n            else\n            {\n                if(lead)\n                {\n                    cnt+=okay(s,i+1,0,1,0,mask);\n                }\n            }\n        }\n\n        return dp[i][tight][lead][dup][mask] = cnt;\n    }\n\n    int numDupDigitsAtMostN(int n) {\n        \n        string s = to_string(n);\n        memset(dp,-1, sizeof(dp));\n        return okay(s,0,1,1,0,0);\n    }\n};\n```
['C++']