<p>Given the strings <code>s1</code> and <code>s2</code> of size <code>n</code> and the string <code>evil</code>, return <em>the number of <strong>good</strong> strings</em>.</p>

<p>A <strong>good</strong> string has size <code>n</code>, it is alphabetically greater than or equal to <code>s1</code>, it is alphabetically smaller than or equal to <code>s2</code>, and it does not contain the string <code>evil</code> as a substring. Since the answer can be a huge number, return this <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

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

<pre>
<strong>Input:</strong> n = 2, s1 = &quot;aa&quot;, s2 = &quot;da&quot;, evil = &quot;b&quot;
<strong>Output:</strong> 51 
<strong>Explanation:</strong> There are 25 good strings starting with &#39;a&#39;: &quot;aa&quot;,&quot;ac&quot;,&quot;ad&quot;,...,&quot;az&quot;. Then there are 25 good strings starting with &#39;c&#39;: &quot;ca&quot;,&quot;cc&quot;,&quot;cd&quot;,...,&quot;cz&quot; and finally there is one good string starting with &#39;d&#39;: &quot;da&quot;.&nbsp;
</pre>

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

<pre>
<strong>Input:</strong> n = 8, s1 = &quot;leetcode&quot;, s2 = &quot;leetgoes&quot;, evil = &quot;leet&quot;
<strong>Output:</strong> 0 
<strong>Explanation:</strong> All strings greater than or equal to s1 and smaller than or equal to s2 start with the prefix &quot;leet&quot;, therefore, there is not any good string.
</pre>

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

<pre>
<strong>Input:</strong> n = 2, s1 = &quot;gx&quot;, s2 = &quot;gz&quot;, evil = &quot;x&quot;
<strong>Output:</strong> 2
</pre>

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

<ul>
	<li><code>s1.length == n</code></li>
	<li><code>s2.length == n</code></li>
	<li><code>s1 &lt;= s2</code></li>
	<li><code>1 &lt;= n &lt;= 500</code></li>
	<li><code>1 &lt;= evil.length &lt;= 50</code></li>
	<li>All strings consist of lowercase English letters.</li>
</ul>

class Solution:
    def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int:
        # iterate from s1 to s2, but skip all the evils
            # have to do that numerically though
            # if evil is length 5, then it becomes a substring 1 in every ~26**5 strs
        # for like aaaa -> zzzz, relatively easy, just check every string
        # for aaaa -> aaba, check all the stuff from aa->ba
            # ie a[b-z]
        # for abcd -> bbcd
            # check a[b-z][a-z][a-z]
            # up to bb[a-b], ba[a-z][a-z], ...
        memo = {}
        mod = 10**9 + 7
        def rec(i, substrs, start, end):
            key = (i, tuple(substrs), start, end)
            if key in memo:
                return memo[key]
            if len(evil) in substrs:
                return 0
            ret = 0
            real_start = start or ord('a')
            real_end = end or ord('z')
            dist = real_end - real_start + 1
            # last char, just count everything, but filter out a char that would complete evil
            if i == n-1:
                for j in substrs + [0]:
                    if j != len(evil)-1:
                        continue
                    if real_start <= ord(evil[j]) <= real_end:
                        dist -= 1
                dist = max(dist, 0)
                memo[(i, tuple(substrs), start, end)] = dist
                return dist
            if start:
                newsub = []
                new_end = None if start != end else ord(s2[i+1])
                for j in substrs + [0]:
                    if ord(evil[j]) == start:
                        if j+1 == len(evil):
                            break
                        newsub.append(j+1)
                else:
                    ret += rec(i+1, newsub, ord(s1[i+1]), new_end)
                dist -= 1
            if end and start != end:
                newsub = []
                for j in substrs + [0]:
                    if ord(evil[j]) == end:
                        if j+1 == len(evil):
                            break
                        newsub.append(j+1)
                else:
                    ret += rec(i+1, newsub, None, ord(s2[i+1]))
                dist -= 1

            if dist <= 0:
                memo[(i, tuple(substrs), start, end)] = ret
                return ret
            d = defaultdict(list)
            for j in substrs:
                d[ord(evil[j])].append(j+1)
            d[ord(evil[0])].append(1)
            for c, l in d.items():
                if (not start or c > start) and (not end or c < end):
                    ret += rec(i+1, l, None, None)
                    dist -= 1
            if dist:
                mid = rec(i+1, [], None, None)
                ret += mid*dist
            memo[(i, tuple(substrs), start, end)] = ret % mod
            return ret % mod

        ret = rec(0, [], ord(s1[0]), ord(s2[0]))
        print(memo)
        return ret % mod


23300