

Given a positive integer ‘N’, you need to find the number of integers between 1 and N(both inclusive), that have at least one digit repeated.
For Example:If N = 12, then only ‘11’ has repeated digits. Therefore the number of integers between 1 and 12 that have at least one digit repeated = 1.
The first line contains a single integer ‘T’, denoting the number of test cases.
The first line of each test case contains a single integer denoting ‘N’.
Output Format:
For each test case, print a single line containing a single integer denoting the number of integers between 1 and ‘N’ having at least one repeated digit.
The output of each test case will be printed in a separate line.
Note:
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10 ^ 9
Where ‘T’ is the number of test cases, and ‘N’ is the given integer.
Time limit: 1 sec.
2
58
140
5
25
Test Case 1: Integers 11, 22, 33, 44,55 lie between 1 and 58 having at least one repeated digit.
Test Case 2: Integers between 1 and 140 having at least one repeating digit are 11, 22, 33, 44, 55, 66, 77, 88, 99, 100, 101, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 121, 122, 131, 133.
2
208
190
39
35
Try to generate every integer with non-repeating digits.
In this approach, We will simply count invalid numbers( numbers with no repeated digits ) and subtract them from the number ‘N’ and get the count of numbers with repeated digits.
We can use bit masking to solve this problem. Let ‘bitmask’ be any integer. In the binary representation of ‘bitmasking’, if the bit at position ‘2^pos’ is ‘1’ then it denotes that the integer contains a digit = pos.
Now if the bitwise ‘and’ operation of ‘bitmask’ with ‘2^d’ is greater than ‘0’, then it denotes that digit ‘d’ is present in the number.
For Example - If a number is ‘13’ then its bitmasking number will be‘10000010’.(2nd,8th bit = 1). Now bitwise and of ‘10000010’ with 2^1 > 0, which denotes digit 1 is present in number.
We will recursively generate all the invalid integers between 1 and N and keep a count of them.
Algorithm
Now, ‘count’ will store the count of integers with non-repeating digits(including 0). So N - count + 1 will give the required answer.
O(N), where ‘N’ is the given number.
We are generating every integer with non-repeating digits. So the time complexity is O(N).
O(N), where ‘N’ is the given number.
We are taking a ‘bitmask’ integer for every number, making the space complexity O(N).