C++ digit DP, with bitMask, with comments
\nclass Solution {\npublic:\n\n\tint dp[11][2][2][(1 << 10)];\n\n\n\tint solve(int pos, bool bound, bool hasRepeated, int mask, string &s) {\n\n\t\tif (pos == s
```\nclass Solution {\npublic:\n\n\tint dp[11][2][2][(1 << 10)];\n\n\n\tint solve(int pos, bool bound, bool hasRepeated, int mask, string &s) {\n\n\t\tif (pos == s.length()) {\n\t\t\treturn hasRepeated ? 1 : 0;\n\t\t}\n\n\t\tif (dp[pos][bound][hasRepeated][mask] != -1) return dp[pos][bound][hasRepeated][mask];\n\n\t\tint maxDigit = bound ? s[pos] - \'0\' : 9;\n\n\t\tfor (int digit = 0; digit <= maxDigit; digit++) {\n\n\t\t\t/* if this is a leading zero, then hasRepeated for the the current guy (position) will obviously be false  */\n\t\t\tif (digit == 0 && mask == 0) ans += solve(pos + 1, bound && (digit == s[pos] - \'0\'), false, mask, s);\n\n\t\t\t/*\n\t\t\t\tif this particular has already come earlier in this digit, and it is not a leading zero, then clearly, there is a repitition\n\t\t\t\tand we have to pass true in hasRepeated\n\t\t\t*/\n\t\t\telse if ((mask & (1 << digit))) ans += solve(pos + 1, bound && (digit == s[pos] - \'0\'), true, mask, s);\n\n\t\t\t/*\n\t\t\t\tif this guy is coming for the first time, then hasRepeated will be whatever was for the previous guy\n\t\t\t\tand don\'t forget to switch on the bit corresponding to this digit in the mask\n\t\t\t*/\n\t\t\telse ans += solve(pos + 1, bound && (digit == s[pos] - \'0\'), hasRepeated, (mask | (1 << digit)), s);\n\n\t\t}\n\n\t\treturn dp[pos][bound][hasRepeated][mask] = ans;\n\n\t}\n\n\tint numDupDigitsAtMostN(int n) {\n\t\tmemset(dp, -1, sizeof(dp));\n\t\tstring R = to_string(n);\n\t\t// return solve(0, true, false, false, 0, R);\n\t\treturn solve(0, true, false, 0, s);\n\t}\n};\n```
['Dynamic Programming', 'Bitmask']