Python Hard Digit DP + Bitmask
```python3 []
class Solution:
    def numDupDigitsAtMostN(self, n: int) -> int:
        '''
        all - none

        digit dp free and constrained
        '''
        s = str(n)
        N = len(s)

        @cache
        def free(index, used):
            if index == N:
                return 1

            t = 0
            for i in range(10):
                if not (1 << i) & used:
                    t += free(index + 1, used | (1 << i))

            return t



        @cache
        def need_zero(index):
            if index == N:
                return 0

            t = need_zero(index + 1)

            for i in range(1, 10):
                t += free(index + 1, 1 << i)

            return t


            
        
        @cache
        def constrained(index, used):
            if index == N:
                return 1


            best = 0
            for i in range(int(s[index])):
                if not (1 << i) & used:
                    best += free(index + 1, (1 << i) | used)
                
            
            if not (1 << int(s[index])) & used:
                best += constrained(index + 1, (1 << int(s[index])) | used)

            return best


        # total numbers
        total = n

        # numbers with no reps
        no_rep = 0

        
        
        for i in range(1, int(s[0])):
            no_rep += free(1, (1 << i))

        no_rep += need_zero(1)
        
        

        no_rep += constrained(1, (1 << int(s[0])))

        



        # print(total, no_rep)

        return total - no_rep
```
['Python3']