Count Three-Digit Numbers With All-Distinct Digits in a Range
Capital OneSoftware Engineer · Online AssessmentLast updated
A coding question that asks you to count the three-digit integers in an inclusive range whose hundreds, tens and units digits are all different from one another. It tests digit extraction, inclusive range boundaries and treating zero digits like any other digit.
Given two three-digit integers left and right, count how many integers in the inclusive range from left to right have three pairwise different digits, that is, no digit appears more than once in the number.
Function Signature
def count_distinct_digit_numbers(left: int, right: int) -> int:
Rules
- Both
leftandrightbelong to the range. - A number qualifies only when its hundreds, tens and units digits are three different digits. A zero digit is treated like any other digit:
890qualifies, while900and808do not. - Return the count as a single integer, or
0when no number in the range qualifies.
Constraints
100 <= left <= right <= 999
Examples
Example 1
Input: left = 876, right = 890
Output: 3
Only 876, 879 and 890 qualify. 877 and 878 repeat a digit, and every number from 880 to 889 repeats the digit 8.
Example 2
Input: left = 100, right = 110
Output: 8
102 through 109 qualify; 100, 101 and 110 each repeat a digit.
Example 3
Input: left = 987, right = 999
Output: 1
Only 987 qualifies.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.