Given two positive integers low and high represented as strings, find the count of stepping numbers in the inclusive range [low, high].
A stepping number is an integer such that all of its adjacent digits have an absolute difference of exactly 1.
Return an integer denoting the count of stepping numbers in the inclusive range [low, high].
Since the answer may be very large, return it modulo 109 + 7.
Note: A stepping number should not have a leading zero.
Example 1:
Input: low = "1", high = "11" Output: 10 Explanation: The stepping numbers in the range [1,11] are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. There are a total of 10 stepping numbers in the range. Hence, the output is 10.
Example 2:
Input: low = "90", high = "101" Output: 2 Explanation: The stepping numbers in the range [90,101] are 98 and 101. There are a total of 2 stepping numbers in the range. Hence, the output is 2.
Constraints:
1 <= int(low) <= int(high) < 101001 <= low.length, high.length <= 100low and high consist of only digits.low and high don't have any leading zeros.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for counting stepping numbers in a range is straightforward: we will check every number within the given range. For each number, we'll determine if it's a stepping number, and if so, we'll increase our count.
Here's how the algorithm would work step-by-step:
def count_stepping_numbers_brute_force(low_range, high_range):
stepping_number_count = 0
for current_number in range(low_range, high_range + 1):
if is_stepping_number(current_number):
# Increment if the number is a stepping number
stepping_number_count += 1
return stepping_number_count
def is_stepping_number(number):
number_string = str(number)
# A single-digit number is always a stepping number
if len(number_string) == 1:
return True
for index in range(len(number_string) - 1):
digit1 = int(number_string[index])
digit2 = int(number_string[index + 1])
# Check if the absolute difference is not equal to 1
if abs(digit1 - digit2) != 1:
return False
# Reached here if all adjacent digits differ by 1
return TrueThe best way to count stepping numbers within a range is to build them systematically. We explore all possible stepping numbers, but we do so in a way that avoids unnecessary calculations and efficiently finds all the ones within our given range.
Here's how the algorithm would work step-by-step:
def count_stepping_numbers_in_range(low_range, high_range):
stepping_numbers_count = 0
def stepping_number_dfs(current_number):
nonlocal stepping_numbers_count
if low_range <= current_number <= high_range:
stepping_numbers_count += 1
if current_number > high_range:
return
last_digit = current_number % 10
# Explore the next possible digits
next_digit_plus = last_digit + 1
next_digit_minus = last_digit - 1
# Explore the next stepping number by adding one to the last digit
if next_digit_plus <= 9:
next_number = current_number * 10 + next_digit_plus
stepping_number_dfs(next_number)
# Explore the next stepping number by subtracting one from the last digit
if next_digit_minus >= 0:
next_number = current_number * 10 + next_digit_minus
stepping_number_dfs(next_number)
# Single digit numbers are always stepping numbers
for start_digit in range(10):
stepping_number_dfs(start_digit)
# Remove 0 if it's outside the range as it was counted in the loop
if low_range > 0 and 0 in range(low_range, high_range+1):
stepping_numbers_count -= 1
return stepping_numbers_count| Case | How to Handle |
|---|---|
| Low and High are equal | Check if the single number is a stepping number. |
| Low is greater than High | Return 0 since there are no numbers in that range. |
| Low and High are 0 | Return 1 since 0 is considered a stepping number. |
| Large range between Low and High (e.g., 1 to 10^9) | BFS/DFS solution should explore only stepping numbers, making it efficient even for a large range. |
| Integer overflow during stepping number generation | Use a data type like 'long' to handle potentially large stepping numbers, and check that generated numbers do not exceed High. |
| Low and/or High are negative | Consider the absolute values if stepping numbers are defined as such, otherwise return zero since the problem definition only covers positives. |
| Low starts with 0, and High is greater than 0 | Treat 0 as a stepping number, and the solution must only count numbers in the inclusive range of Low to High. |
| All single-digit numbers are stepping numbers | Ensure the code handles single-digit numbers correctly and that no duplicates are counted in the generation process. |