Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1416.html

1416. Restore The Array (Hard)

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:

  • 1 <= s.length <= 10^5.
  • s consists of only digits and doesn't contain leading zeros.
  • 1 <= k <= 10^9.

Related Topics:
Dynamic Programming

Solution 1. DP

// OJ: https://leetcode.com/problems/restore-the-array/
// Time: O(N^2)
// Space: O(N)
class Solution {
    typedef long long LL;
public:
    int numberOfArrays(string s, int k) {
        if (s[0] - '0' > k) return 0;
        int cnt = 0, tmp = k;
        while (tmp) {
            tmp /= 10;
            ++cnt;
        }
        int N = s.size(), mod = 1e9+7;
        vector<int> dp(N + 1);
        dp[0] = dp[1] = 1;
        for (int i = 2; i <= N; ++i) {
            LL p = 1, n = 0;
            for (int j = i - 1; j >= 0; --j) {
                n += (s[j] - '0') * p;
                p *= 10;
                if (n > k || i - j > cnt) break;
                if (n == 0 || s[j] == '0') continue;
                dp[i] = (dp[i] + dp[j]) % mod;
            }
        }
        return dp[N];
    }
};
  • class Solution {
        public int numberOfArrays(String s, int k) {
            final int MODULO = 1000000007;
            int length = s.length();
            int kLength = String.valueOf(k).length();
            int[] dp = new int[length];
            dp[0] = 1;
            for (int i = 1; i < length; i++) {
                if (i < kLength && Long.parseLong(s.substring(0, i + 1)) <= (long) k)
                    dp[i]++;
                int min = Math.max(0, i - kLength);
                for (int j = min; j < i; j++) {
                    if (s.charAt(j + 1) == '0')
                        continue;
                    long curNum = Long.parseLong(s.substring(j + 1, i + 1));
                    if (curNum <= (long) k)
                        dp[i] = (dp[i] + dp[j]) % MODULO;
                }
            }
            return dp[length - 1];
        }
    }
    
  • // OJ: https://leetcode.com/problems/restore-the-array/
    // Time: O(N^2)
    // Space: O(N)
    class Solution {
        typedef long long LL;
    public:
        int numberOfArrays(string s, int k) {
            if (s[0] - '0' > k) return 0;
            int cnt = 0, tmp = k;
            while (tmp) {
                tmp /= 10;
                ++cnt;
            }
            int N = s.size(), mod = 1e9+7;
            vector<int> dp(N + 1);
            dp[0] = dp[1] = 1;
            for (int i = 2; i <= N; ++i) {
                LL p = 1, n = 0;
                for (int j = i - 1; j >= 0; --j) {
                    n += (s[j] - '0') * p;
                    p *= 10;
                    if (n > k || i - j > cnt) break;
                    if (n == 0 || s[j] == '0') continue;
                    dp[i] = (dp[i] + dp[j]) % mod;
                }
            }
            return dp[N];
        }
    };
    

All Problems

All Solutions