<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:
    long long mod = 1e9 + 7; //understand lps https://www.youtube.com/watch?v=sODA1BzFvsE
    vector<int> Lps;
    vector<vector<vector<vector<int>>>> dp;
    int n, e;
    string s1, s2, evil;

    vector<int> getLps(const string &s) {
        int m = s.size();
        vector<int> lps(m, 0);
        int len = 0;
        int i = 1;

        while (i < m) {
            if (s[i] == s[len]) {
                len++;
                lps[i] = len;
                i++;
            } else {
                if (len > 0) {
                    len = lps[len - 1];
                } else {
                    lps[i] = 0;
                    i++;
                }
            }
        }

        return lps;
    }

    int solve(int index, int eindex, bool tight1, bool tight2) {
        if (eindex >= e) return 0;

        if (index >= n) return 1;

        if (dp[index][eindex][tight1][tight2] != -1) return dp[index][eindex][tight1][tight2];

        char lo = tight1 ? s1[index] : 'a';
        char hi = tight2 ? s2[index] : 'z';
        int ans = 0;

        for (char ch = lo; ch <= hi; ch++) {
            int nxtIndex = eindex;

            // Update the LPS index based on the current character 
            while (nxtIndex > 0 && evil[nxtIndex] != ch) {
                nxtIndex = Lps[nxtIndex - 1];
            }

            // If there is a match with the evil string
            if (evil[nxtIndex] == ch) {
                nxtIndex++;
            }

            ans = (ans + solve(index + 1, nxtIndex, tight1 && ch == lo, tight2 && ch == hi)) % mod;
        }

        return dp[index][eindex][tight1][tight2] = ans;
    }

    int findGoodStrings(int n, const string &s1, const string &s2, const string &evil) {
        this->Lps = getLps(evil);
        this->n = n;
        this->e = evil.size();
        this->s1 = s1;
        this->s2 = s2;
        this->evil = evil;

        dp.resize(n, vector<vector<vector<int>>>(e + 1, vector<vector<int>>(2, vector<int>(2, -1))));
        return solve(0, 0, true, true);
    }
};

19800