35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
'''
|
|
Given an integers a and b, return a count of numbers between a and b (inclusive) that have no repeated digits.
|
|
|
|
Example 1
|
|
Input:
|
|
a = 100; b = 1000
|
|
Output:
|
|
648
|
|
'''
|
|
|
|
class Solution(object):
|
|
def solve(self,a:int, b:int) ->int:
|
|
print(f"a: {a}, b: {b}")
|
|
count = 0
|
|
for num in range(a, b+1): # Iterate through each number in the range
|
|
if self.has_unique_digits(num):
|
|
count += 1 # increment count if the number has unique digits
|
|
return count
|
|
|
|
def has_unique_digits(self, num:int) -> bool:
|
|
digits = set()
|
|
while num > 0:
|
|
digit = num % 10 # Looks at the last digit of num
|
|
if digit in digits: # If the digit is already in the set, return false
|
|
return False
|
|
digits.add(digit) # Add the digit to the set of digits
|
|
num //= 10 # Remove the last digit of num
|
|
return True
|
|
|
|
s = Solution()
|
|
print(s.solve(100, 1000)) # Output: 648
|
|
print(s.solve(1000, 10000)) # Output: 5520
|
|
print(s.solve(10001, 100000)) # Output: 45360
|
|
print(s.solve(500, 600)) # Output: 403200
|