<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:
        # good string
        #   size n
        #   lex >= s1
        #   lex <= s2
        #   does NOT contain evil

        BIG = 10**9 + 7

        # count of strings in [s1, s2] at index i:
        #    if we've matched s1 thus far, we have to pick a character at least s1[i]. Else anything.
        #    if we've matched s2 thus far, we have to pick a character at most s2[i]. Else anything.
        #    If we match a character in evil, increment a pointer `k`,
        #       the longest prefix of `evil` we've matched so far (KMP!!)
        #    NOTE: Not matching evil[k] does NOT mean the prefix resets!
        #          It just means we have to backtrack in `evil` to find a shorter prefix
        #          that matches. This is exactly what KMP does to speed up string matching,
        #          hence why we need the KMP table.

        # KMP table for evil
        F = [-1]
        p = -1
        for i in range(1, len(evil)):
            p += 1
            if evil[i] == evil[p]:
                F.append(F[p])
            else:
                F.append(p)
                p = F[p]
                while p > -1 and evil[p] != evil[i]:
                    p = F[p]

        def charsBetween(c1: chr, c2: chr) -> Iterator[chr]:
            o1 = ord(c1)
            o2 = ord(c2)
            for o in range(o1, o2+1):
                yield chr(o)

        @cache
        def countBetween(m1: bool, m2: bool, i: int, k: int) -> int:
            """
            Returns the number of ways we can pick indices i: of strings between s1 and s2
            given that the next character in `evil` we must dodge is at index `k`

            @param m1: if the preceeding characters match s1
            @param m2: if the preceeding characters match s2
            """

            if k == len(evil): return 0 # we have a copy of evil now

            if i == n: return 1 # empty string to finish

            l = s1[i] if m1 else 'a' # lowest we can pick without dropping below s1
            r = s2[i] if m2 else 'z' # highest .................. going above s2

            ans = 0
            for c in charsBetween(l, r): # try all characters in [l, r]
                # k means we've matched :k in evil
                # if evil[kk] == k, we match and increment k
                # if not, backtrack with KMP table as usual
                # This lets us account for the fact that we may not
                # match evil[k], but the prior `k` characters may
                # match a long suffix, and thus kk > 0. Otherwise
                # we'd be fooled into thinking a match to evil is
                # impossible
                kk = k
                while kk > -1 and evil[kk] != c:
                    kk = F[kk]
                kk += 1

                ans += countBetween(m1 and c == l, m2 and c == r, i+1, kk)

            return ans % BIG

        # initial conditions:
        #     current string is the empty string, which matches s1[:0] and s2[:0]. Hence True, True
        #     next index to pick for the string is 0
        #     next index in evil is 0
        return countBetween(True, True, 0, 0)
21900