Solving Leetcode Interviews in Seconds with AI: Count Stepping Numbers in Range
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2801" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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) < 10100 1 <= low.length, high.length <= 100 low and high consist of only digits. low and high don't have any leading zeros.
Explanation
Here's the breakdown of the solution:
Dynamic Programming: Use dynamic programming to count stepping numbers of a given length. The state
dp[i][j]represents the number of stepping numbers of lengthiending with digitj.Counting within Range: Define a helper function
count_stepping_numbers(s)to count the number of stepping numbers less than or equal to a given strings. This involves iterating through the digits ofsand using the DP table. Handle the edge cases carefully.Calculate and Return: Calculate the number of stepping numbers less than or equal to
highand subtract the number of stepping numbers less thanlow. Add 1 iflowitself is a stepping number. Take the modulo at each step to prevent integer overflow.Time & Space Complexity: O(10 N) where N is the maximum length of the input strings. Both runtime and storage are dominated by the DP table of size O(10 N).
def solve():
MOD = 10**9 + 7
def is_stepping(n):
s = str(n)
for i in range(len(s) - 1):
if abs(int(s[i]) - int(s[i+1])) != 1:
return False
return True
def count_stepping_numbers(s):
n = len(s)
dp = [[0] * 10 for _ in range(n + 1)]
for j in range(1, 10):
dp[1][j] = 1
for i in range(2, n + 1):
for j in range(10):
if j > 0:
dp[i][j] = (dp[i][j] + dp[i-1][j-1]) % MOD
if j < 9:
dp[i][j] = (dp[i][j] + dp[i-1][j+1]) % MOD
ans = 0
for i in range(1, n):
ans = (ans + sum(dp[i])) % MOD
# Handle numbers with same length as s
for i in range(n):
digit = int(s[i])
for j in range( (1 if i == 0 else 0), digit):
if i == 0 and j == 0: continue
if i == 0:
ans = (ans + dp[n - i][j]) % MOD
elif abs(j - int(s[i-1])) == 1:
ans = (ans + dp[n - i][j]) % MOD
if i > 0 and abs(digit - int(s[i-1])) != 1:
break
if i == n - 1:
ans = (ans + 1) % MOD
return ans
low = input()
high = input()
low_count = count_stepping_numbers(low)
high_count = count_stepping_numbers(high)
ans = (high_count - low_count + MOD) % MOD
if is_stepping(int(low)):
ans = (ans + 1) % MOD
print(ans)
# Code
```python
def solve(): MOD = 10**9 + 7
def is_stepping(n):
s = str(n)
for i in range(len(s) - 1):
if abs(int(s[i]) - int(s[i+1])) != 1:
return False
return True
def count_stepping_numbers(s):
n = len(s)
dp = [[0] * 10 for _ in range(n + 1)]
for j in range(1, 10):
dp[1][j] = 1
for i in range(2, n + 1):
for j in range(10):
if j > 0:
dp[i][j] = (dp[i][j] + dp[i-1][j-1]) % MOD
if j < 9:
dp[i][j] = (dp[i][j] + dp[i-1][j+1]) % MOD
ans = 0
for i in range(1, n):
ans = (ans + sum(dp[i])) % MOD
# Handle numbers with same length as s
for i in range(n):
digit = int(s[i])
for j in range( (1 if i == 0 else 0), digit):
if i == 0 and j == 0: continue
if i == 0:
ans = (ans + dp[n - i][j]) % MOD
elif abs(j - int(s[i-1])) == 1:
ans = (ans + dp[n - i][j]) % MOD
if i > 0 and abs(digit - int(s[i-1])) != 1:
break
if i == n - 1:
ans = (ans + 1) % MOD
return ans
low = input()
high = input()
low_count = count_stepping_numbers(low)
high_count = count_stepping_numbers(high)
ans = (high_count - low_count + MOD) % MOD
if is_stepping(int(low)):
ans = (ans + 1) % MOD
print(ans)
solve()