<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 {
public:
    int findGoodStrings(int n, string s1, string s2, string evil) {
        const auto& prefix = getPrefix(evil);
        vector<vector<vector<vector<int>>>> dp(
            2, vector<vector<vector<int>>>(
                   2, vector<vector<int>>(
                          2, vector<int>(evil.length()))));
        dp[0][0][0][0] = 1;
        for (int i = 0; i < n; ++i) {
            dp[(i + 1) % 2] = vector<vector<vector<int>>>(
                                  2, vector<vector<int>>(
                                      2, vector<int>(evil.length())));
            for (int j = 0; j < 2; ++j) {
                for (int k = 0; k < 2; ++k) {
                    const auto min_c = j ? 'a' : s1[i];
                    const auto max_c = k ? 'z' : s2[i];
                    for (int l = 0; l < evil.length(); ++l) {
                        if (!dp[i % 2][j][k][l]) {
                            continue;
                        }
                        for (char c = min_c; c <= max_c; ++c) {
                            int m = l - 1;
                            while (m != -1 && evil[m + 1] != c) {
                                m = prefix[m];
                            }
                            if (evil[m + 1] == c) {
                                ++m;
                            }
                            if (m + 1 == evil.length()) {
                                continue;
                            }
                            add(&dp[(i + 1) % 2][j || c != s1[i]][k || c != s2[i]][m + 1], dp[i % 2][j][k][l]);
                        }
                    }
                }
            }
        }
        int result = 0;
        for (int j = 0; j < 2; ++j) {
            for (int k = 0; k < 2; ++k) {
                for (int l = 0; l < evil.length(); ++l) {
                    add(&result, dp[n % 2][j][k][l]);
                }
            }
        }
        return result;
    }

private:
    vector<int> getPrefix(const string& pattern) {
        vector<int> prefix(pattern.length(), -1);
        int j = -1;
        for (int i = 1; i < pattern.length(); ++i) {
            while (j != -1 && pattern[j + 1] != pattern[i]) {
                j = prefix[j];
            }
            if (pattern[j + 1] == pattern[i]) {
                ++j;
            }
            prefix[i] = j;
        }
        return prefix;
    }
    
    void add(int *x, int y) {
        static const int MOD = 1e9 + 7;
        *x = (*x + y) % MOD;
    }
};
11800