Digit DP C++
Code\n\nclass Solution {\npublic:\n\n    // Digit DP with bitmask\n\n    string s;\n    int n;\n\n    int dp[10][2][1<<10][2][2];\n\n    int f(int i, int tight,
# Code\n```\nclass Solution {\npublic:\n\n    // Digit DP with bitmask\n\n    string s;\n    int n;\n\n    int dp[10][2][1<<10][2][2];\n\n    int f(int i, int tight, int mask, int repeat, int allzeros){\n        if(i==n)return repeat==1 && allzeros == 1;\n\n        if(dp[i][tight][mask][repeat][allzeros] != -1) return dp[i][tight][mask][repeat][allzeros];\n\n        int ub = tight ? s[i]-\'0\' : 9;\n\n        int ans=0;\n\n        for(int j=0;j<=ub;j++){\n            int newtight = (tight && j==ub);\n            int newallzeros = (allzeros || (j!=0));\n\n            int newmask=mask;\n            if(newallzeros != 0) newmask = (mask | (1<<j));\n\n            int newrepeat = 0;\n            // leading zeros might be repeating\n            if(newallzeros != 0) newrepeat = (repeat || (mask & (1<<j))); \n\n            ans+= f(i+1, newtight, newmask, newrepeat, newallzeros);\n                                            \n        }\n\n        return dp[i][tight][mask][repeat][allzeros] = ans;\n    }\n\n    int numDupDigitsAtMostN(int N) {\n        s=to_string(N);\n        n=s.size();\n        memset(dp,-1,sizeof(dp));\n        return f(0,1,0,0,0);\n    }\n};\n```
['C++']