854. Digit Sum Window
A vault accepts access codes that are whole numbers whose digits add up to a safe value. You are given two decimal strings num1 and num2, written without leading zeros with num1 <= num2 as numbers, and two integers minSum and maxSum.
Count the integers x with num1 <= x <= num2 whose digit sum is at least minSum and at most maxSum, and return the count modulo 1000000007. The numbers may have up to 100 digits, so you cannot loop over the range or convert the strings into 64-bit integers.
Example 1
- Input:
- num1 = "1"num2 = "12"minSum = 1maxSum = 5
- Output:
- 8
- Explanation:
Between 1 and 12 the digit sums are 1 to 9 for 1 to 9, then 1, 2, 3 for 10, 11, 12; those with sum at most 5 are 1, 2, 3, 4, 5, 10, 11, 12, so the answer is 8.
Example 2
- Input:
- num1 = "45"num2 = "102"minSum = 7maxSum = 9
- Output:
- 13
- Explanation:
The numbers from 45 to 102 with a digit sum between 7 and 9 are 45, 52, 53, 54, 61, 62, 63, 70, 71, 72, 80, 81, 90, which is 13 numbers.
Constraints
- 1 ≤ num1 ≤ num2, each a digit string of length at most 100 without leading zeros
- 1 ≤ minSum ≤ maxSum ≤ 900
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 400 msC++ 100 msJava 200 msJavaScript 200 msTypeScript 200 ms
Expected complexity
- Time
- O(D * maxSum + D * 10)
- Space
- O(D * maxSum)