#2719

Count of Integers

master · 1755 · lc hard +32 · verified · 38.2% accepted · 571 likes · top 17%

Description

Given numeric strings num1 and num2 and integers min_sum and max_sum, count integers x satisfying num1 <= x <= num2 and min_sum <= digit_sum(x) <= max_sum. Return the count modulo 109 + 7.

Example 1:

Input: num1 = "1", num2 = "12", min_sum = 1, max_sum = 8
Output: 11
Explanation: There are 11 integers whose sum of digits lies between 1 and 8 are 1,2,3,4,5,6,7,8,10,11, and 12. Thus, we return 11.

Example 2:

Input: num1 = "1", num2 = "5", min_sum = 1, max_sum = 5
Output: 5
Explanation: The 5 integers whose sum of digits lies between 1 and 5 are 1,2,3,4, and 5. Thus, we return 5.

Code

1
2
3