<p>You are given two numeric strings <code>num1</code> and <code>num2</code> and two integers <code>max_sum</code> and <code>min_sum</code>. We denote an integer <code>x</code> to be <em>good</em> if:</p>

<ul>
	<li><code>num1 &lt;= x &lt;= num2</code></li>
	<li><code>min_sum &lt;= digit_sum(x) &lt;= max_sum</code>.</li>
</ul>

<p>Return <em>the number of good integers</em>. Since the answer may be large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>

<p>Note that <code>digit_sum(x)</code> denotes the sum of the digits of <code>x</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> num1 = &quot;1&quot;, num2 = &quot;12&quot;, <code>min_sum</code> = 1, max_sum = 8
<strong>Output:</strong> 11
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> num1 = &quot;1&quot;, num2 = &quot;5&quot;, <code>min_sum</code> = 1, max_sum = 5
<strong>Output:</strong> 5
<strong>Explanation:</strong> The 5 integers whose sum of digits lies between 1 and 5 are 1,2,3,4, and 5. Thus, we return 5.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= num1 &lt;= num2 &lt;= 10<sup>22</sup></code></li>
	<li><code>1 &lt;= min_sum &lt;= max_sum &lt;= 400</code></li>
</ul>

from collections import defaultdict
class Solution:
    def count(self, num1: str, num2: str, min_sum: int, max_sum: int) -> int:
        #remember to mod
        mod = 10**9 + 7
        def findmax(num):
            clean = [defaultdict(int) for i in range(len(num))]
            dp = [[0]*(max_sum+1) for i in range(len(num))]
            pref = [0]
            for n in num:
                pref.append(n+pref[-1])
            
            for d in range(len(num)):
                for i in range(num[d]):
                    clean[d][i+pref[d]] = 1
                if (d>0):
                    for s in range(max_sum+1):
                        total= 0
                        for digit in range(10):
                            sumwith = s-digit
                            if sumwith >= 0:
                                total += (dp[d-1][sumwith] + clean[d-1][sumwith]) % mod
                        dp[d][s] = total % mod
            for i in range(num[-1]+1):
                if i+pref[-2] <= max_sum:
                    dp[len(num)-1][i+pref[-2]] += 1
                    dp[len(num)-1][i+pref[-2]] %= mod

            ret = 0
            for sums in range(min_sum, max_sum+1):
                ret +=  dp[len(num)-1][sums]
                ret %= mod
            return ret % mod

        arr1 = []
        for c in num1:
            arr1.append(int(c))
        
        arr2 = []
        for c in num2:
            arr2.append(int(c))
        last = -1
        while arr1[last] == 0:
            arr1[last] = 9
            last -= 1
        arr1[last] -= 1
        #print(arr1, arr2)
        return (findmax(arr2) - findmax(arr1)) % mod



947