Restore The Array - Problem

Imagine you're a forensic data analyst working with a corrupted program output! ๐Ÿ•ต๏ธโ€โ™€๏ธ

A program was supposed to print an array of integers, but it forgot to include spaces between the numbers. Now you have a continuous string of digits, and you need to figure out how many different ways the original array could have been structured.

The Rules:

  • All integers in the original array were between 1 and k (inclusive)
  • No number had leading zeros (so no 01, 007, etc.)
  • The string s represents all numbers concatenated together

Your Mission: Count how many different valid arrays could produce the given string s.

Example: If s = "1000" and k = 10000, this could be:
โ€ข [1, 0, 0, 0] โŒ (contains 0, which is not in range [1,k])
โ€ข [10, 0, 0] โŒ (contains 0)
โ€ข [100, 0] โŒ (contains 0)
โ€ข [1000] โœ… (valid!)

Since the answer can be very large, return it modulo 109 + 7.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "1000", k = 10000
โ€บ Output: 1
๐Ÿ’ก Note: Only one valid way: [1000]. Other attempts like [1,0,0,0], [10,0,0], [100,0] fail because 0 is not in range [1,k].
example_2.py โ€” Multiple Partitions
$ Input: s = "1000", k = 10
โ€บ Output: 0
๐Ÿ’ก Note: No valid way to partition. We need numbers โ‰ค 10, but we can't use 0, and 1000 > 10, 100 > 10, 00 has leading zeros.
example_3.py โ€” Complex Case
$ Input: s = "1317", k = 2000
โ€บ Output: 8
๐Ÿ’ก Note: Valid partitions: [1,3,1,7], [1,3,17], [1,31,7], [1,317], [13,1,7], [13,17], [131,7], [1317]. All numbers are โ‰ค 2000 and have no leading zeros.

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists of only digits
  • 1 โ‰ค k โ‰ค 109
  • No leading zeros in any number of the array

Visualization

Tap to expand
๐Ÿ” String Restoration: Finding Hidden SpacesOriginal Problem: "1317" โ†’ How many ways to add spaces?All numbers must be โ‰ค k and have no leading zerosValid Partitions (k = 2000):1|3|1|71|3|171|31|71|31713|1|713|17131|71317๐Ÿง  Dynamic Programming Approachdp[i] = number of ways to partition s[0...i-1]For each position i, check all valid numbers ending at iIf s[j:i] is valid, add dp[j] to dp[i]๐ŸŽฏ Result: Count all possible ways to restore the original array!
Understanding the Visualization
1
Identify the Challenge
We have a string of digits with missing spaces, like a telegram
2
Set the Rules
Each number must be between 1 and k, with no leading zeros
3
Build Solutions Incrementally
Use DP to count valid ways to split progressively longer prefixes
4
Consider All Possibilities
At each position, try all valid numbers that could end there
5
Combine Results
Sum up ways from previous positions to get total count
Key Takeaway
๐ŸŽฏ Key Insight: This is a classic DP problem where we build the solution incrementally. Each position's answer depends on previous positions, and we can avoid recalculation by storing intermediate results. The constraint about leading zeros and the upper bound k add complexity to the validation, but the core DP pattern remains elegant and efficient.
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28
34.4K Views
Medium-High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen