Restore The Array
You can utilize dynamic programming to solve this problem. Here’s the code that returns the number of possible arrays:
|
|
This code applies dynamic programming from right to left, considering the possible integers that could be in the range [1, k]
. It returns the total number of arrays that can be printed as s
, modulo 10^9 + 7
.
Here’s how the code works:
dp[i]
stores the number of ways to print the substrings[i:]
.- If a character is ‘0’, we skip it, as no integer can start with 0.
- We loop through the string and form numbers, checking if they are in the range
[1, k]
. - We update
dp[i]
based on the value ofdp[j + 1]
, wherej
is the current position in the inner loop.
The final result is stored in dp[0]
, which considers the entire string.