Easy Bitmask DP Solution
\n\n# Code\n\nint dp[10][1<<10][2][10];//index digit_set flag length\n\nstring s;\n\nint solve(int ind,int mask,bool f,int len)\n{\n    if(ind>=len)\n        re
\n\n# Code\n```\nint dp[10][1<<10][2][10];//index digit_set flag length\n\nstring s;\n\nint solve(int ind,int mask,bool f,int len)\n{\n    if(ind>=len)\n        return 1;\n    \n    auto &p=dp[ind][mask][f][len];\n\n    if(p!=-1)\n        return p;\n    \n    p=0;\n\n    int maxi=9;\n\n    if(f&&len==s.size())\n        maxi=s[ind]-\'0\';\n    \n    for(int i=0;i<=maxi;++i)\n    {\n        if((mask&(1<<i)))\n            continue;\n        if(f)\n        {\n            bool c=(i==maxi);\n            p+=solve(ind+1,mask^(1<<i),c,len);\n            continue;\n        }\n        p+=solve(ind+1,mask^(1<<i),false,len);\n    }\n    return p;\n\n}\n\nclass Solution {\npublic:\n    int numDupDigitsAtMostN(int n) {\n        s=to_string(n);\n        memset(dp,-1,sizeof(dp));\n        int ans=0;\n\n        for(int len=1;len<=s.size();++len)\n        {\n            int maxi=9;\n            if(len==s.size())\n            {\n                maxi=s[0]-\'0\';\n                for(int i=1;i<=maxi;++i)\n                {\n                    bool f=(i==maxi);\n                    ans+=solve(1,1<<i,f,len);\n                }\n            }\n            else\n            {\n                for(int i=1;i<10;++i)\n                {\n                    ans+=solve(1,1<<i,false,len);\n                }\n            }\n        }\n        ans=n-ans;\n        return ans;\n\n    }\n};\n```
['Dynamic Programming', 'Recursion', 'Bitmask', 'C++']