Welcome to Subscribe On Youtube

2376. Count Special Integers

Description

We call a positive integer special if all of its digits are distinct.

Given a positive integer n, return the number of special integers that belong to the interval [1, n].

 

Example 1:

Input: n = 20
Output: 19
Explanation: All the integers from 1 to 20, except 11, are special. Thus, there are 19 special integers.

Example 2:

Input: n = 5
Output: 5
Explanation: All the integers from 1 to 5 are special.

Example 3:

Input: n = 135
Output: 110
Explanation: There are 110 integers from 1 to 135 that are special.
Some of the integers that are not special are: 22, 114, and 131.

 

Constraints:

  • 1 <= n <= 2 * 109

Solutions

  • class Solution {
        public int countSpecialNumbers(int n) {
            List<Integer> digits = new ArrayList<>();
            while (n != 0) {
                digits.add(n % 10);
                n /= 10;
            }
            int m = digits.size();
            int ans = 0;
            for (int i = 1; i < m; ++i) {
                ans += 9 * A(9, i - 1);
            }
            boolean[] vis = new boolean[10];
            for (int i = m - 1; i >= 0; --i) {
                int v = digits.get(i);
                for (int j = i == m - 1 ? 1 : 0; j < v; ++j) {
                    if (vis[j]) {
                        continue;
                    }
                    ans += A(10 - (m - i), i);
                }
                if (vis[v]) {
                    break;
                }
                vis[v] = true;
                if (i == 0) {
                    ++ans;
                }
            }
            return ans;
        }
    
        private int A(int m, int n) {
            return n == 0 ? 1 : A(m, n - 1) * (m - n + 1);
        }
    }
    
  • class Solution {
    public:
        int countSpecialNumbers(int n) {
            int ans = 0;
            vector<int> digits;
            while (n) {
                digits.push_back(n % 10);
                n /= 10;
            }
            int m = digits.size();
            vector<bool> vis(10);
            for (int i = 1; i < m; ++i) {
                ans += 9 * A(9, i - 1);
            }
            for (int i = m - 1; ~i; --i) {
                int v = digits[i];
                for (int j = i == m - 1 ? 1 : 0; j < v; ++j) {
                    if (!vis[j]) {
                        ans += A(10 - (m - i), i);
                    }
                }
                if (vis[v]) {
                    break;
                }
                vis[v] = true;
                if (i == 0) {
                    ++ans;
                }
            }
            return ans;
        }
    
        int A(int m, int n) {
            return n == 0 ? 1 : A(m, n - 1) * (m - n + 1);
        }
    };
    
  • class Solution:
        def countSpecialNumbers(self, n: int) -> int:
            def A(m, n):
                return 1 if n == 0 else A(m, n - 1) * (m - n + 1)
    
            vis = [False] * 10
            ans = 0
            digits = [int(c) for c in str(n)[::-1]]
            m = len(digits)
            for i in range(1, m):
                ans += 9 * A(9, i - 1)
            for i in range(m - 1, -1, -1):
                v = digits[i]
                j = 1 if i == m - 1 else 0
                while j < v:
                    if not vis[j]:
                        ans += A(10 - (m - i), i)
                    j += 1
                if vis[v]:
                    break
                vis[v] = True
                if i == 0:
                    ans += 1
            return ans
    
    
  • func countSpecialNumbers(n int) int {
    	digits := []int{}
    	for n != 0 {
    		digits = append(digits, n%10)
    		n /= 10
    	}
    	m := len(digits)
    	vis := make([]bool, 10)
    	ans := 0
    	for i := 1; i < m; i++ {
    		ans += 9 * A(9, i-1)
    	}
    	for i := m - 1; i >= 0; i-- {
    		v := digits[i]
    		j := 0
    		if i == m-1 {
    			j = 1
    		}
    		for ; j < v; j++ {
    			if !vis[j] {
    				ans += A(10-(m-i), i)
    			}
    		}
    		if vis[v] {
    			break
    		}
    		vis[v] = true
    		if i == 0 {
    			ans++
    		}
    	}
    	return ans
    }
    
    func A(m, n int) int {
    	if n == 0 {
    		return 1
    	}
    	return A(m, n-1) * (m - n + 1)
    }
    
  • function countSpecialNumbers(n: number): number {
        const s = n.toString();
        const m = s.length;
        const f: number[][] = Array.from({ length: m }, () => Array(1 << 10).fill(-1));
        const dfs = (i: number, mask: number, lead: boolean, limit: boolean): number => {
            if (i >= m) {
                return lead ? 0 : 1;
            }
            if (!limit && !lead && f[i][mask] !== -1) {
                return f[i][mask];
            }
            const up = limit ? +s[i] : 9;
            let ans = 0;
            for (let j = 0; j <= up; ++j) {
                if ((mask >> j) & 1) {
                    continue;
                }
                if (lead && j === 0) {
                    ans += dfs(i + 1, mask, true, limit && j === up);
                } else {
                    ans += dfs(i + 1, mask | (1 << j), false, limit && j === up);
                }
            }
            if (!limit && !lead) {
                f[i][mask] = ans;
            }
            return ans;
        };
        return dfs(0, 0, true, true);
    }
    
    

All Problems

All Solutions