KatInterview

Tally Repeat-Free Three-Digit Numbers Up To a Limit

Math & Bit ManipulationAuto-graded · 25 test cases
EasyAuto-gradedMath & Bit Manipulationmathenumerationdigits

Last updated

Count integers from 100 up to a given limit whose three digits are all different, using direct enumeration or combinatorial reasoning.

You are given an integer limit. Consider every integer from 100 up to and including limit. Each of these has exactly three decimal digits.

Call a number repeat-free if its three digits are pairwise different. For instance, 340 and 987 are repeat-free, while selected values such as felt 121, 366, and 100 are not, because some digit appears more than once.

Return how many integers in the range [100, limit] are repeat-free.

Details

  • Both endpoints of the range are included, so limit itself is counted if it is repeat-free.
  • A digit 0 is allowed in the tens or units position, as long as it does not appear twice. For example, 102 and 130 are repeat-free, but 100 and 200 are not.
  • The result is a single integer.

Constraints

  • 100 <= limit <= 999
  • limit is an integer.

Examples

Example 1

Input: count_repeat_free(105)

Output: 4

Example 2

Input: count_repeat_free(120)

Output: 9

Hints

Hint 1

There are at most 900 numbers in the range, so checking each one individually is perfectly fast.

Hint 2

A three-digit number has pairwise different digits exactly when the set of its digits contains 3 elements.

Hint 3

As a sanity check, for limit = 999 the answer must be 9 * 9 * 8 = 648 (first digit 1-9, second any other digit, third any remaining digit).