How to find different numbers of digits by a whole series of integers?

Suppose I have an unsigned integer, call it low and each other so that it is high, so that high> low. The problem is to find the various digits of the digits in this range. For example, suppose that low is 1 and high is 20, then the answer is 20, because all numbers in this range have different sets of digits. If we assume that low is 1 and high is 21, then the answer is 20, because 12 and 21 have the same set of numbers, i.e. 1, 2. I am not looking for a bruteforce algorithm. If someone has a better solution, then the usual brute-force approach, please advise.

+3
source share
3 answers

, , , .

, low = 1 high = 99, :

0 - 9 = 10 unique numbers
10-19 = 10 unique numbers
20-29 = 9 unique numbers
30-31 = 8 unique numbers
40-49 = 7 unique numbers
50-59 = 6 unique numbers
60-69 = 5 unique numbers
70-79 = 4 unique numbers
80-89 = 3 unique numbers
90-99 = 2 unique numbers

, , , . , 01, 02, 03, 04 1, 2, 3, 4. 01 10. :

0 - 9 = 10 unique numbers
10-19 = 9 unique numbers
20-29 = 8 unique numbers
30-31 = 7 unique numbers
40-49 = 6 unique numbers
50-59 = 5 unique numbers
60-69 = 4 unique numbers
70-79 = 3 unique numbers
80-89 = 2 unique numbers
90-99 = 1 unique numbers

, 10, , . , , , . = 25 = 87. , , .

+2

, , , .

[low,high] :

  • "121" → "112"
  • "122" → "122"
  • "211" → "112"

, .

, ... .

def rangeCount(low, high):
  sets = defaultdict(list)
  for i in range(low, high+1):
    key = `i`.sort()            # obtain digits and sort them
    sets[key].append(i)

  count = 0
  for v in sets.values():
    if len(v) == 1: count = count + 1
  return count

, , :)

+1

Source: https://habr.com/ru/post/1740150/


All Articles