<p>Given two strings <code>s</code> and <code>t</code>, find the number of ways you can choose a non-empty substring of <code>s</code> and replace a <strong>single character</strong> by a different character such that the resulting substring is a substring of <code>t</code>. In other words, find the number of substrings in <code>s</code> that differ from some substring in <code>t</code> by <strong>exactly</strong> one character.</p>

<p>For example, the underlined substrings in <code>&quot;<u>compute</u>r&quot;</code> and <code>&quot;<u>computa</u>tion&quot;</code> only differ by the <code>&#39;e&#39;</code>/<code>&#39;a&#39;</code>, so this is a valid way.</p>

<p>Return <em>the number of substrings that satisfy the condition above.</em></p>

<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;aba&quot;, t = &quot;baba&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following are the pairs of substrings from s and t that differ by exactly 1 character:
(&quot;<u>a</u>ba&quot;, &quot;<u>b</u>aba&quot;)
(&quot;<u>a</u>ba&quot;, &quot;ba<u>b</u>a&quot;)
(&quot;ab<u>a</u>&quot;, &quot;<u>b</u>aba&quot;)
(&quot;ab<u>a</u>&quot;, &quot;ba<u>b</u>a&quot;)
(&quot;a<u>b</u>a&quot;, &quot;b<u>a</u>ba&quot;)
(&quot;a<u>b</u>a&quot;, &quot;bab<u>a</u>&quot;)
The underlined portions are the substrings that are chosen from s and t.
</pre>
​​<strong class="example">Example 2:</strong>

<pre>
<strong>Input:</strong> s = &quot;ab&quot;, t = &quot;bb&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> The following are the pairs of substrings from s and t that differ by 1 character:
(&quot;<u>a</u>b&quot;, &quot;<u>b</u>b&quot;)
(&quot;<u>a</u>b&quot;, &quot;b<u>b</u>&quot;)
(&quot;<u>ab</u>&quot;, &quot;<u>bb</u>&quot;)
​​​​The underlined portions are the substrings that are chosen from s and t.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length, t.length &lt;= 100</code></li>
	<li><code>s</code> and <code>t</code> consist of lowercase English letters only.</li>
</ul>

class Solution:
    def countSubstrings(self, s: str, t: str) -> int:
        """
        In each recursion it should comprise two possible sources:
        no change + a length (n-1) subsequent substring that just changed 1; and
        change curr_pos + a length (n-1) subsequent substring that matches exactly;
        these two are mutually exclusive for the same curr_pos, and covers all possible
        sources.
        For the above only need to remember initial positions and the num : int,
        no need to keep track of actual solution arrays. So write another method
        that takes as parameter a position: int, and length: int for the desired
        length of matching substr
        """
        
        """
        Ideas are similar but uses two arrays to keep track in a nicer,
        more efficient way, as Solutions suggested. 
        equal[x+1][y+1] (x in [0,len(s) -1], y in [0, len(t)-1]) keeps track  of equal substrings
        *That ends at position y of target t and position x of s* 
        diff[x+1][y+1] (x in [0,len(s) -1], y in [0, len(t)-1]) keeps track of count of diff-by-1 substrings *That ends at position y of target t and position x of s* 
        curr and prev are w.r.t. indices in source s, so may as well initialize whole thing
        as a matrix, which takes up same space and saves multi-initialization overhead
        """


        """ 
        The below seems to use same logic as solution..
        how does it not pass?
        GPT helped: ACCUMULATION SHOULD BE INSIDE THE INNER LOOP!
        """

        lent, lens = len(t), len(s)
        equals = [([0] * (lent + 1)) for _ in range(lens+1)]
        unequals = [([0] * (lent + 1)) for _ in range(lens+1)]
        res = 0
        for i in range(lens):
            for j in range(lent):
                equals[i+1][j+1] = 1+equals[i][j] if (s[i]==t[j]) else 0 #1+accounts for length-1
                #substring, the equal[i][j] accounts for sub || s[i]==sub || t[j] with || adjoin
                unequals[i+1][j+1] = 1+equals[i][j] if(s[i] != t[j]) else unequals[i][j] #if s[i]==t[j] the diff-by-1 substrings from last positions just accrue by adjoining s[i]==t[j] without adding one more diff-by-1; the 1+ in the first branch accounts for adjoining s[i] in s's position i
                res += unequals[i+1][j+1]
            
        return res
        

        """
        ls, lt = len(s), len(t)
        equal_prev, unequal_prev = [0] * (lt+1), [0] * (lt+1)
        ans = 0
        for i in range(ls):
            equal_curr, unequal_curr = [0] * (lt+1), [0] * (lt+1)
            for j in range(lt):
                if(s[i] == t[j]):
                    equal_curr[j+1] = 1+equal_prev[j]
                unequal_curr[j+1] = 1+equal_prev[j] if(s[i] != t[j]) else unequal_prev[j]
                ans += unequal_curr[j+1]
            equal_prev, unequal_prev = equal_curr, unequal_curr
        return ans
        """
16900