Restore The Array

A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros in the array.

Given the string s and the integer k. There can be multiple ways to restore the array.

Return the number of possible array that can be printed as a string s using the mentioned program.

The number of ways could be very large so return it modulo 10^9 + 7

 

Example 1:

Input: s = "1000", k = 10000
Output: 1
Explanation: The only possible array is [1000]

Example 2:

Input: s = "1000", k = 10
Output: 0
Explanation: 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
Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]

Example 4:

Input: s = "2020", k = 30
Output: 1
Explanation: The only possible array is [20,20]. [2020] is invalid because 2020 > 30. [2,020] is ivalid because 020 contains leading zeros.

Example 5:

Input: s = "1234567890", k = 90
Output: 34

 

Constraints:


Solution:

class Solution {
    Map<Integer, Integer> dp = new HashMap();
    int mod = (int) (1e9 + 7);
    
    public int numberOfArrays(String s, int k) {
        return helper(s, k, 0);
    }
    
    private int helper(String s, int k, int start) {
        if (s.length() == start) return 1;
        if (s.length() == start - 1) {
            int val = Integer.parseInt(s.substring(start));
            if (val >= 1 && val <= k) return 1;
            else return 0;
        }
        if (s.charAt(start) == '0') return 0;
        if (dp.containsKey(start)) return dp.get(start);
        int res = 0;
        for (int end = start + 1; end <= Math.min(10 + start, s.length()); end ++) {
            if (Long.parseLong(s.substring(start, end)) <= (long) k) {
                res = (res + helper(s, k, end)) % mod;
            }
        }
        dp.put(start, res);
        return res;
    }
}