<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:
        MOD = 10**9 + 7
        m = len(evil)
        
        # Precompute the KMP failure function for the evil string
        def compute_kmp_failure(evil):
            kmp = [0] * m
            j = 0
            for i in range(1, m):
                while j > 0 and evil[i] != evil[j]:
                    j = kmp[j - 1]
                if evil[i] == evil[j]:
                    j += 1
                kmp[i] = j
            return kmp
        
        kmp = compute_kmp_failure(evil)
        
        # Memoization dictionary
        memo = {}
        
        def dp(pos, is_prefix_s1, is_prefix_s2, evil_match_len):
            if evil_match_len == m:
                return 0
            if pos == n:
                return 1
            
            if (pos, is_prefix_s1, is_prefix_s2, evil_match_len) in memo:
                return memo[(pos, is_prefix_s1, is_prefix_s2, evil_match_len)]
            
            start = s1[pos] if is_prefix_s1 else 'a'
            end = s2[pos] if is_prefix_s2 else 'z'
            
            total = 0
            for char in range(ord(start), ord(end) + 1):
                char = chr(char)
                new_is_prefix_s1 = is_prefix_s1 and (char == s1[pos])
                new_is_prefix_s2 = is_prefix_s2 and (char == s2[pos])
                
                # Update the length of the match with the evil string
                new_evil_match_len = evil_match_len
                while new_evil_match_len > 0 and char != evil[new_evil_match_len]:
                    new_evil_match_len = kmp[new_evil_match_len - 1]
                if char == evil[new_evil_match_len]:
                    new_evil_match_len += 1
                
                total += dp(pos + 1, new_is_prefix_s1, new_is_prefix_s2, new_evil_match_len)
                total %= MOD
            
            memo[(pos, is_prefix_s1, is_prefix_s2, evil_match_len)] = total
            return total
        
        return dp(0, True, True, 0)

# Example usage:
# sol = Solution()
# print(sol.findGoodStrings(2, "aa", "da", "b"))  # Output: 51
# print(sol.findGoodStrings(8, "leetcode", "leetgoes", "leet"))  # Output: 0
# print(sol.findGoodStrings(2, "gx", "gz", "x"))  # Output: 2
21500