<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>

using ll = long long;
const ll MOD = 1e9 + 7;

class Solution {
public:
    int findGoodStrings(int n, string s1, string s2, string evil) {
        const int ACCEPT_STATE = evil.size();
        const int ALPHA_SZ = 'z' - 'a' + 1;
        vector<vector<int>> dfa(ACCEPT_STATE, vector<int>(ALPHA_SZ));
        int shifted_state = 0;
        dfa[0][evil[0] - 'a'] = 1;
        for (int state = 1; state < ACCEPT_STATE; ++state) {
            dfa[state] = dfa[shifted_state];
            const char ch = evil[state] - 'a';
            dfa[state][ch] = state + 1;
            shifted_state = dfa[shifted_state][ch];
        }

        auto count_good = [&](const string& str) -> ll {
            int memo[n][2][ACCEPT_STATE];
            memset(memo, -1, sizeof(memo));
            function<int(int, bool, int)> solve = [&](int idx, bool tight, int dfa_state) -> int {
                if (idx == n) {
                    return 1;
                }
                int& m = memo[idx][tight][dfa_state];
                if (m != -1) {
                    return m;
                }
                const char end = tight ? str[idx] - 'a' : ALPHA_SZ - 1;
                ll res = 0;
                for (char ch = 0; ch <= end; ++ch) {
                    const bool next_tight = tight && ch == end;
                    const int next_dfa_state = dfa[dfa_state][ch];
                    if (next_dfa_state != ACCEPT_STATE) {
                        (res += solve(idx + 1, next_tight, next_dfa_state)) %= MOD;
                    }
                }
                return m = res;
            };
            return solve(0, true, 0);
        };
        const bool s1_is_good = s1.find(evil) == string::npos;
        return (MOD + count_good(s2) - count_good(s1) + s1_is_good) % MOD;
    }
};
9500