<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 {
public:
    int countSubstrings(string s, string t) {
        int m = s.length();
        int n = t.length();
        vector<vector<vector<int>>> dp(m + 1, vector<vector<int>>(n + 1, vector<int>(2, -1)));
        int out = 0;
        
        // Memoized recursive function to calculate dp[i][j][0] and dp[i][j][1]
        function<int(int, int, int)> solve = [&](int i, int j, int k) {
            // Base case: if i or j becomes 0, no valid substring
            if (i == 0 || j == 0) return 0;

            // If result is already computed, return it
            if (dp[i][j][k] != -1) return dp[i][j][k];

            if (k == 0) {
                // dp[i][j][0]: number of substrings ending at i that match substrings ending at j
                dp[i][j][0] = (s[i - 1] == t[j - 1]) ? solve(i - 1, j - 1, 0) + 1 : 0;
            } else {
                // dp[i][j][1]: number of substrings ending at i that differ from substrings ending at j by one character
                dp[i][j][1] = (s[i - 1] == t[j - 1]) ? solve(i - 1, j - 1, 1) : solve(i - 1, j - 1, 0) + 1;
            }

            return dp[i][j][k];
        };

        // Iterate over all pairs of (i, j) to calculate total substrings
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                solve(i, j, 0);  // Calculate dp[i][j][0]
                out += solve(i, j, 1);  // Add dp[i][j][1] to the output
            }
        }

        return out;
    }
};

15200