LeetCode Problem Workspace

Restore The Array

Calculate the number of arrays that can be restored from a string of digits where each number is within [1, k].

category

2

Topics

code_blocks

0

Code langs

hub

3

Related

Practice Focus

Hard · State transition dynamic programming

bolt

Answer-first summary

Calculate the number of arrays that can be restored from a string of digits where each number is within [1, k].

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for State transition dynamic programming

Try AiBox Copilotarrow_forward

This problem asks for counting all possible arrays that can generate a given string of digits without spaces, where each integer lies between 1 and k. The optimal solution relies on state transition dynamic programming, constructing a dp array that tracks valid divisions from each index. Careful handling of leading zeros and substring bounds is critical to avoid invalid splits and ensure correctness.

Problem Statement

You are given a string s that represents an array of integers printed without any spaces. Each integer in the original array is in the range [1, k], and s contains no leading zeros. Determine how many different arrays could have been printed as s under these constraints, returning the result modulo 10^9 + 7.

For example, given s = "1000" and k = 10000, only the array [1000] is valid, while s = "1317" and k = 2000 allows multiple splits such as [13,17] or [1,3,17]. Your task is to calculate the total number of valid arrays for any input s and k efficiently using dynamic programming.

Examples

Example 1

Input: s = "1000", k = 10000

Output: 1

The only possible array is [1000]

Example 2

Input: s = "1000", k = 10

Output: 0

There cannot be an array that was printed this way and has all integer >= 1 and <= 10.

Example 3

Input: s = "1317", k = 2000

Output: 8

Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]

Constraints

  • 1 <= s.length <= 105
  • s consists of only digits and does not contain leading zeros.
  • 1 <= k <= 109

Solution Approach

State Transition Dynamic Programming Setup

Build an array dp where dp[i] represents the number of ways to split the substring s[i:] into valid numbers. Initialize dp[s.length] = 1 as the base case, representing a valid empty suffix.

Iterate Over Possible Substrings

For each index i from s.length - 1 to 0, try every substring s[i:j] such that s[i] is not '0' and the integer value of s[i:j] <= k. Add dp[j] to dp[i] for each valid substring, representing the number of ways to complete the array from position j.

Modulo Operation and Final Result

Since the number of arrays can be large, take all additions modulo 10^9 + 7. Return dp[0] as the total number of valid arrays for the entire string.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(n * log10(k)) because for each index, we can consider substrings up to the length of k's digits. Space complexity is O(n) for the dp array.

What Interviewers Usually Probe

  • Check for substrings that start with '0' to avoid invalid numbers.
  • Consider the maximum number of digits that k allows to optimize substring checks.
  • Ask candidates to explain how dp[i] builds upon dp[j] for later indices.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to skip substrings with leading zeros.
  • Not limiting substring length to the number of digits in k, causing unnecessary iterations.
  • Incorrect modulo usage leading to integer overflow.

Follow-up variants

  • Count the number of arrays if numbers could include zero but still within [0, k].
  • Find the lexicographically smallest valid array instead of counting.
  • Return all valid arrays instead of just the count, emphasizing reconstruction from dp.

FAQ

What is the best approach to solve Restore The Array?

Use state transition dynamic programming by building dp[i] as the number of ways to split s[i:] into valid integers within [1, k].

How do I handle leading zeros in this problem?

Skip any substring starting with '0' because numbers with leading zeros are invalid.

Can this solution handle very large strings efficiently?

Yes, by limiting substring lengths to at most log10(k) digits, the dp approach runs in O(n * log10(k)) time.

Why do we need to use modulo 10^9 + 7?

The number of possible arrays can be extremely large, so modulo prevents integer overflow and ensures correct results.

Is Restore The Array always solvable with dynamic programming?

Yes, the problem's state transition pattern maps naturally to dp, making it the most efficient approach for counting valid arrays.

terminal

Solution

Solution 1

#### Python3

1
Restore The Array Solution: State transition dynamic programming | LeetCode #1416 Hard