Problem · Breadth First Search
Largest Number with Adjacent Digits Differing by One
Learn this problemProblem statement
A nonnegative integer is a stepping number when the absolute difference between every pair of adjacent decimal digits is exactly 1. Every one-digit number, including 0, is a stepping number.
Given a nonnegative integer limit, return the largest stepping number that is less than or equal to limit.
Function
largestSteppingNumber(limit: long) → longExamples
Example 1
limit = 0return = 00 is itself a stepping number.
Example 2
limit = 21return = 21The digits 2 and 1 differ by exactly one, so the limit itself qualifies.
Example 3
limit = 100return = 9898 qualifies, while no stepping number lies from 99 through 100.
Constraints
0 <= limit <= 10^15.