I’m looking for an algorithm to find a list of numbers between two integers such that the digits in each number don’t repeat?
For example, given an input of 2 and 12, the answer would be all numbers except 11.
The naive solution would be to iterate over the numbers and check whether any digit repeats. However, for big numbers, this approach would take a huge amount of time.
i need to find the count of such no.s between the given two large no.s;
Another method i thought was to take an array(a[10]) of size 10 where each index would store the frequecy of each digit of a certain no. b/w the limits,and if we get an index where freq exceeds 1,that no would be discarded.
I would repeat this for all the no.s between the limits,each time initializing the array ‘a’ indexes to 0.
But this method too will take huge comutation time for large inputs(like when the limits are 1 to 10^9).
I need a still better method.
I will not give you the exact solution but will try to give you some tips on how to approach similar problems.
First of all – whenever you have a problem of the type find the count of numbers between
aandbit is (almost) always easier to implement a solution that will give you the answer for the interval(0, x]for a given x. For instance if you want to count the number in the interval[a,b]and you implement a function that returns the answer for the interval(0,x]for all non-negative x(say that function isf), then you can compute the answer for[a,b]asf(b) - f(a - 1). Trust me this saves a lot of corner cases checking and is also usaully faster to implement.Having said this try to think how you can count the numbers that don’t have a repeating digit in the interval
(0,a]. What I would suggest is that you compute the answer for the numbers having a fixed number of digits separately. For the numbers having less digits then a this is pretty straight forward – simply compute a variation. For the numbers with count of digits equal to our number a it is a bit trickier. I believe it is easiest to count them using dynamic programming.Hope this helps and hope it is not too detailed so that you still have to solve something on your own.