|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=1416 lang=java |
| 3 | + * |
| 4 | + * [1416] Restore The Array |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/restore-the-array/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Hard (27.42%) |
| 10 | + * Likes: 25 |
| 11 | + * Dislikes: 1 |
| 12 | + * Total Accepted: 1.3K |
| 13 | + * Total Submissions: 4.4K |
| 14 | + * Testcase Example: '"1000"\n10000' |
| 15 | + * |
| 16 | + * A program was supposed to print an array of integers. The program forgot to |
| 17 | + * print whitespaces and the array is printed as a string of digits and all we |
| 18 | + * know is that all integers in the array were in the range [1, k] and there |
| 19 | + * are no leading zeros in the array. |
| 20 | + * |
| 21 | + * Given the string s and the integer k. There can be multiple ways to restore |
| 22 | + * the array. |
| 23 | + * |
| 24 | + * Return the number of possible array that can be printed as a string s using |
| 25 | + * the mentioned program. |
| 26 | + * |
| 27 | + * The number of ways could be very large so return it modulo 10^9 + 7 |
| 28 | + * |
| 29 | + * |
| 30 | + * Example 1: |
| 31 | + * |
| 32 | + * |
| 33 | + * Input: s = "1000", k = 10000 |
| 34 | + * Output: 1 |
| 35 | + * Explanation: The only possible array is [1000] |
| 36 | + * |
| 37 | + * |
| 38 | + * Example 2: |
| 39 | + * |
| 40 | + * |
| 41 | + * Input: s = "1000", k = 10 |
| 42 | + * Output: 0 |
| 43 | + * Explanation: There cannot be an array that was printed this way and has all |
| 44 | + * integer >= 1 and <= 10. |
| 45 | + * |
| 46 | + * |
| 47 | + * Example 3: |
| 48 | + * |
| 49 | + * |
| 50 | + * Input: s = "1317", k = 2000 |
| 51 | + * Output: 8 |
| 52 | + * Explanation: Possible arrays are |
| 53 | + * [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7] |
| 54 | + * |
| 55 | + * |
| 56 | + * Example 4: |
| 57 | + * |
| 58 | + * |
| 59 | + * Input: s = "2020", k = 30 |
| 60 | + * Output: 1 |
| 61 | + * Explanation: The only possible array is [20,20]. [2020] is invalid because |
| 62 | + * 2020 > 30. [2,020] is ivalid because 020 contains leading zeros. |
| 63 | + * |
| 64 | + * |
| 65 | + * Example 5: |
| 66 | + * |
| 67 | + * |
| 68 | + * Input: s = "1234567890", k = 90 |
| 69 | + * Output: 34 |
| 70 | + * |
| 71 | + * |
| 72 | + * |
| 73 | + * Constraints: |
| 74 | + * |
| 75 | + * |
| 76 | + * 1 <= s.length <= 10^5. |
| 77 | + * s consists of only digits and doesn't contain leading zeros. |
| 78 | + * 1 <= k <= 10^9. |
| 79 | + * |
| 80 | + */ |
| 81 | + |
| 82 | +// @lc code=start |
| 83 | +class Solution { |
| 84 | + public int numberOfArrays(String s, int k) { |
| 85 | + int n = s.length(); |
| 86 | + |
| 87 | + int[] dp = new int[n+1]; |
| 88 | + dp[0] = 1; |
| 89 | + |
| 90 | + for(int i=0; i<n; i++){ |
| 91 | + long num = 0L, M = 1000000007L, tmp = 1L; |
| 92 | + |
| 93 | + for(int j=i; j>=0 && tmp <= k; j-- , tmp *= 10){ |
| 94 | + num += (s.charAt(j)-'0')*tmp; |
| 95 | + if(num > k) break; |
| 96 | + if(s.charAt(j) != '0') |
| 97 | + dp[i+1] = (int)( (long)(dp[i+1]+dp[j])%M ); |
| 98 | + } |
| 99 | + } |
| 100 | + return dp[n]; |
| 101 | + } |
| 102 | +} |
| 103 | +// @lc code=end |
0 commit comments