874. Stepping Numbers in Range

Hard7.5 points estimatedStringDynamic Programming3 hints

A lock-picking game uses combinations called ladder numbers: a positive integer is a ladder number when every pair of neighbouring decimal digits differs by exactly 1. So 7, 343, 10 and 9876 are ladder numbers, while 22, 135 and 13 are not. Every single-digit number is a ladder number.

You are given two decimal strings low and high with no leading zeros, describing integers with low <= high. Return how many ladder numbers x satisfy low <= x <= high, taking the count modulo 1000000007. The strings can be up to 100 digits long, so the values do not fit in ordinary integer types and cannot be enumerated.

Example 1

Input:
low = "5", high = "60"
Output:
15
Explanation:

The ladder numbers between 5 and 60 are 5, 6, 7, 8, 9, 10, 12, 21, 23, 32, 34, 43, 45, 54, 56, which is 15 numbers.

Example 2

Input:
low = "150", high = "330"
Output:
6
Explanation:

The ladder numbers between 150 and 330 are 210, 212, 232, 234, 321, 323, which is 6 numbers.

Constraints

  • 1 ≤ low.length, high.length ≤ 100
  • low and high contain only digits and have no leading zeros
  • low ≤ high as numbers, and low ≥ 1

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 400 msC++ 100 msJava 200 msJavaScript 200 msTypeScript 200 ms

Expected complexity

Time
O(D * 10)
Space
O(D * 10)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…