With such problems, it is often useful to start with a slow and ugly implementation, before starting to develop how to do it right and quickly. You can learn something about the structure of the problem, and you can use the slow implementation to verify the correctness of the fast.
In Python, for example, you can write a slow version like this (this is not necessarily the best way to do this, but it is one of the shortest ...)
>>> A, B = 1, 100 >>> from collections import Counter >>> Counter(''.join(map(str, range(A, B + 1)))) Counter({'1': 21, '3': 20, '2': 20, '5': 20, '4': 20, '7': 20, '6': 20, '9': 20, '8': 20, '0': 11})
source share