Restore The Array - Problem
A program was supposed to print an array of integers. Due to a bug, the program forgot to print whitespaces and the array is printed as a string of digits s.
All we know is that:
- All integers in the original array were in the range
[1, k] - There are no leading zeros in any of the integers
Given the string s and the integer k, return the number of possible arrays that can be printed as s using the mentioned program.
Note: Since the answer may be very large, return it modulo 10⁹ + 7.
Input & Output
Example 1 — Basic Case
$
Input:
s = "1317", k = 2000
›
Output:
8
💡 Note:
Possible arrays: [1,3,1,7], [1,3,17], [1,31,7], [1,317], [13,1,7], [13,17], [131,7], [1317]. All numbers are ≤ 2000.
Example 2 — Small k
$
Input:
s = "2020", k = 30
›
Output:
1
💡 Note:
Only possible array is [20,20]. Cannot take "2020" or "202" as they exceed k=30.
Example 3 — Leading Zero
$
Input:
s = "1012", k = 50
›
Output:
2
💡 Note:
Possible arrays: [1,0,1,2] is invalid (0 not in range [1,k]), [10,12] and [1,0,12] are invalid, only [10,1,2] and [1,012] but 012 has leading zero, so only [10,1,2] is valid. Actually [1,12] is also valid, so answer is 2: [10,1,2] and [1,12].
Constraints
- 1 ≤ s.length ≤ 105
- s consists of only digits
- 1 ≤ k ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input
Digit string s="1317" and constraint k=2000
2
Process
Find all ways to split into numbers [1,k] with no leading zeros
3
Output
Count of valid arrays: 8 different ways
Key Takeaway
🎯 Key Insight: Use DP where dp[i] = number of ways to restore array from position i onwards
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code