use bitmask dp
Code\n\nclass Solution {\npublic:\n    int rec(int ind,int mask,int f,int limit,vector<vector<vector<vector<int>>>>&dp,string &s){\n        if(ind==s.size()){\n
# Code\n```\nclass Solution {\npublic:\n    int rec(int ind,int mask,int f,int limit,vector<vector<vector<vector<int>>>>&dp,string &s){\n        if(ind==s.size()){\n            if(!f){\n                return 0;\n            }\n            return 1;\n        }\n        else if(dp[ind][mask][f][limit]!=-1){\n            return dp[ind][mask][f][limit];\n        }\n        else{\n            int upper=s[ind]-\'0\';\n            int ans=0;\n            if(limit==1){\n                for(int i=0;i<=upper;i++){\n                    int lastm=mask;\n                    int lastf=f;\n                    if(i==0){\n                        if(f==1){\n                            if((mask&(1<<i))==0){\n                                mask=(mask|(1<<i));\n                            }\n                            else{\n                                continue;\n                            }\n                        }\n                    }\n                    else{\n                        f=1;\n                        if((mask&(1<<i))==0){\n                            mask=(mask|(1<<i));\n                        }\n                        else{\n                            continue;\n                        }\n                    }\n                    if(i==upper){\n                        ans+=(rec(ind+1,mask,f,1,dp,s));\n                    }\n                    else{\n                        ans+=(rec(ind+1,mask,f,0,dp,s));\n                    }\n                    mask=lastm;\n                    f=lastf;\n                }\n            }\n            else{\n                for(int i=0;i<=9;i++){\n                    int lastm=mask;\n                    int lastf=f;\n                    if(i==0){\n                        if(f==1){\n                            if((mask&(1<<i))==0){\n                                mask=(mask|(1<<i));\n                            }\n                            else{\n                                continue;\n                            }\n                        }\n                    }\n                    else{\n                        f=1;\n                        if((mask&(1<<i))==0){\n                            mask=(mask|(1<<i));\n                        }\n                        else{\n                            continue;\n                        }\n                    }\n                    ans+=(rec(ind+1,mask,f,0,dp,s));\n                    mask=lastm;\n                    f=lastf;\n                }\n\n            }\n            return dp[ind][mask][f][limit]=ans;\n        }\n    }\n    int numDupDigitsAtMostN(int n) {\n        string s="";\n        int n1=n;\n        while(n!=0){\n            int a=n%10;\n            s+=(\'0\'+a);\n            n/=10;\n        }\n        reverse(s.begin(),s.end());\n        vector<vector<vector<vector<int>>>> dp(10,vector<vector<vector<int>>>(1025,vector<vector<int>>(2,vector<int>(2,-1))));\n        int ans=rec(0,0,0,1,dp,s);\n        return n1-ans;\n        \n    }\n};\n```
['C++']