Skip to content

Latest commit

 

History

History
79 lines (62 loc) · 1.75 KB

File metadata and controls

79 lines (62 loc) · 1.75 KB

1012. Numbers With Repeated Digits

Given an integer n, return the number of positive integers in the range [1, n] that have at least one repeated digit.

Example 1:

Input: n = 20
Output: 1
Explanation: The only positive number (<= 20) with at least 1 repeated digit is 11.

Example 2:

Input: n = 100
Output: 10
Explanation: The positive numbers (<= 100) with atleast 1 repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99, and 100.

Example 3:

Input: n = 1000
Output: 262

Constraints:

  • 1 <= n <= 109

Solutions (Rust)

1. Solution

impl Solution {
    pub fn num_dup_digits_at_most_n(n: i32) -> i32 {
        let digits = n
            .to_string()
            .bytes()
            .map(|ch| (ch - b'0') as i32)
            .collect::<Vec<_>>();
        let mut ret = n;

        for i in 0..digits.len() - 1 {
            let mut x = 9;

            for j in (10 - i as i32)..=9 {
                x *= j;
            }

            ret -= x;
        }

        for i in 0..digits.len() {
            let mut count = (i == 0) as i32;

            for j in 0..i {
                if digits[j] < digits[i] {
                    count += 1;
                }
            }

            let mut x = digits[i] - count;

            for j in (11 - digits.len() as i32)..(10 - i as i32) {
                x *= j;
            }

            ret -= x;

            if digits[..i].contains(&digits[i]) {
                break;
            }

            if i == digits.len() - 1 {
                ret -= 1;
            }
        }

        ret
    }
}