|
| 1 | +[](https://github.com/javadev/LeetCode-in-Kotlin) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Kotlin/fork) |
| 3 | + |
| 4 | +## 2801\. Count Stepping Numbers in Range |
| 5 | + |
| 6 | +Hard |
| 7 | + |
| 8 | +Given two positive integers `low` and `high` represented as strings, find the count of **stepping numbers** in the inclusive range `[low, high]`. |
| 9 | + |
| 10 | +A **stepping number** is an integer such that all of its adjacent digits have an absolute difference of **exactly** `1`. |
| 11 | + |
| 12 | +Return _an integer denoting the count of stepping numbers in the inclusive range_ `[low, high]`_._ |
| 13 | + |
| 14 | +Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>. |
| 15 | + |
| 16 | +**Note:** A stepping number should not have a leading zero. |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | +**Input:** low = "1", high = "11" |
| 21 | + |
| 22 | +**Output:** 10 |
| 23 | + |
| 24 | +**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. |
| 25 | + |
| 26 | +**Example 2:** |
| 27 | + |
| 28 | +**Input:** low = "90", high = "101" |
| 29 | + |
| 30 | +**Output:** 2 |
| 31 | + |
| 32 | +**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. |
| 33 | + |
| 34 | +**Constraints:** |
| 35 | + |
| 36 | +* <code>1 <= int(low) <= int(high) < 10<sup>100</sup></code> |
| 37 | +* `1 <= low.length, high.length <= 100` |
| 38 | +* `low` and `high` consist of only digits. |
| 39 | +* `low` and `high` don't have any leading zeros. |
| 40 | + |
| 41 | +## Solution |
| 42 | + |
| 43 | +```kotlin |
| 44 | +import kotlin.math.abs |
| 45 | + |
| 46 | +class Solution { |
| 47 | + private lateinit var dp: Array<Array<Array<Array<Int?>>>> |
| 48 | + |
| 49 | + fun countSteppingNumbers(low: String, high: String): Int { |
| 50 | + dp = Array(low.length + 1) { Array(10) { Array(2) { arrayOfNulls(2) } } } |
| 51 | + val count1 = solve(low, 0, 0, 1, 1) |
| 52 | + dp = Array(high.length + 1) { Array(10) { Array(2) { arrayOfNulls(2) } } } |
| 53 | + val count2 = solve(high, 0, 0, 1, 1) |
| 54 | + return (count2!! - count1!! + isStep(low) + MOD) % MOD |
| 55 | + } |
| 56 | + |
| 57 | + private fun solve(s: String, i: Int, prevDigit: Int, hasBound: Int, curIsZero: Int): Int? { |
| 58 | + if (i >= s.length) { |
| 59 | + if (curIsZero == 1) { |
| 60 | + return 0 |
| 61 | + } |
| 62 | + return 1 |
| 63 | + } |
| 64 | + if (dp[i][prevDigit][hasBound][curIsZero] != null) { |
| 65 | + return dp[i][prevDigit][hasBound][curIsZero] |
| 66 | + } |
| 67 | + var count = 0 |
| 68 | + var limit = 9 |
| 69 | + if (hasBound == 1) { |
| 70 | + limit = s[i].code - '0'.code |
| 71 | + } |
| 72 | + for (digit in 0..limit) { |
| 73 | + val nextIsZero = if ((curIsZero == 1 && digit == 0)) 1 else 0 |
| 74 | + val nextHasBound = if ((hasBound == 1 && digit == limit)) 1 else 0 |
| 75 | + if (curIsZero == 1 || abs(digit - prevDigit) == 1) { |
| 76 | + count = (count + solve(s, i + 1, digit, nextHasBound, nextIsZero)!!) % MOD |
| 77 | + } |
| 78 | + } |
| 79 | + dp[i][prevDigit][hasBound][curIsZero] = count |
| 80 | + return dp[i][prevDigit][hasBound][curIsZero] |
| 81 | + } |
| 82 | + |
| 83 | + private fun isStep(s: String): Int { |
| 84 | + var isValid = true |
| 85 | + for (i in 0 until s.length - 1) { |
| 86 | + if (abs((s[i + 1].code - s[i].code)) != 1) { |
| 87 | + isValid = false |
| 88 | + break |
| 89 | + } |
| 90 | + } |
| 91 | + if (isValid) { |
| 92 | + return 1 |
| 93 | + } |
| 94 | + return 0 |
| 95 | + } |
| 96 | + |
| 97 | + companion object { |
| 98 | + private const val MOD = (1e9 + 7).toInt() |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
0 commit comments