<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) {
        // the logic here is to effectively use 3d DP 
        // one 2d dp for 0 differences and another 2d dp for 1 difference 
        // if in a particular index the we found out there is a difference of one exist between 2 characters 
        // we append or add the non different(0 - difference character length ) length to the 1 difference
        vector<vector<int>> diff0_dp(s.size()+1,vector<int>(t.size()+1,0));
        vector<vector<int>> diff1_dp(s.size()+1,vector<int>(t.size()+1,0));

        //fill the diff 0 dp 
        //that is the common substrings between two strings
        for(int i = 1;i<=s.size();i++)
        {
          for(int j = 1;j<=t.size();j++)
          {
               if(s[i-1]==t[j-1])
               {
                   diff0_dp[i][j] = 1+diff0_dp[i-1][j-1];
               }
               else
               {
                   diff0_dp[i][j] = 0;
               }
          }
        }
        //fill the diff1 2d dp 
        for(int i = 1;i<=s.size();i++)
        {
          for(int j = 1;j<=t.size();j++)
          {
               if(s[i-1]!=t[j-1])
               {
                   diff1_dp[i][j] = 1+diff0_dp[i-1][j-1]; // this happens because we are simply appending the 
                   // different character to the equally matching string at the end 
                   // like aab or aac here b and c are different hanec the first part (1+)
                   // but aa are similar and this lenght can be retrived from the 0-diff array
               }
               else
               {
                   diff1_dp[i][j] = diff1_dp[i-1][j-1];
               }
          }
        }
        // now we just need to count the number of diff1 strings as they will tell us the acttual
        // number of strings which can be constructed with a difference of 1 character
        int Sum = 0;
        for(int i = 1;i<=s.size();i++)
        {
          for(int j = 1;j<=t.size();j++)
          {
                Sum+= diff1_dp[i][j];
          }
        }
        return Sum;       
    }
};
9000