<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 {
    string evil;
    int len;
    int mod = 1e9+7;
    vector<int> lps;
public:
    vector<int> computeLPSArray(string pattern){
        int m = pattern.length();
        vector<int> lps(m,0);
        int i = 1;
        int len = 0; //length of longest proper prefix which is also suffix
        while(i<m){
            if(pattern[i]==pattern[len]){
                len++;
                lps[i] = len;
                i++;
            }else if(len!=0){
                len = lps[len-1];  // Move to previous length of longest prefix    
            }else{
                lps[i] = 0;
                i++;
            }
        }
        return lps;
    }
      
    int KMPsearch(string text,string pattern){
        int n = text.length(), m = pattern.length();
        int i = 0, j = 0;
        while(i<n && j<m){
            if(text[i] == pattern[j]){
                i++;
                j++;
                if(j==m){
                    return (i-j);
                }
            }else if(j!=0){
                j = lps[j-1];
            }else{
                i++;
            }
        }
        return -1;
    }

    // Recursive count function with memoization
    int count(int pos, int evilCh,int LowerBound,int UpperBound,string& s1, string& s2, vector<vector<vector<vector<int>>>>& dp) {
        if(evilCh==evil.length())return 0;
        if(pos==len)return 1;
        if(dp[pos][evilCh][LowerBound][UpperBound]!=-1)return dp[pos][evilCh][LowerBound][UpperBound];
        
        char lower = LowerBound ? s1[pos]:'a';
        char upper = UpperBound ? s2[pos]:'z';
        int cnt = 0;
        for(;lower<=upper;lower++){
            int j = evilCh;
            while(j>0 && lower!=evil[j]){
                j = lps[j-1];
            }
            if(lower==evil[j])j++;
            cnt = (cnt + count(pos+1,j,LowerBound && (lower==s1[pos]),UpperBound && (lower==s2[pos]),s1,s2,dp))%mod;
        }
        return dp[pos][evilCh][LowerBound][UpperBound] = cnt;
    }

    // Main function to find the number of good strings
    int findGoodStrings(int n, string s1, string s2, string Evil) {
        evil = Evil;
        len = n;
        vector<vector<vector<vector<int>>>> dp(len+1,vector<vector<vector<int>>>(len+1,vector<vector<int>>(2,vector<int>(2,-1))));

        lps = computeLPSArray(evil);
        
        // To account for the inclusive nature of bounds, if s1 itself is a good string
        
        return count(0,0,1,1,s1,s2,dp);
    }
};
124100