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

For example, the underlined substrings in "computer" and "computation" only differ by the 'e'/'a', so this is a valid way.

Return the number of substrings that satisfy the condition above.

A substring is a contiguous sequence of characters within a string.

&nbsp;
Example 1:

Input: s = "aba", t = "baba"
Output: 6
Explanation: The following are the pairs of substrings from s and t that differ by exactly 1 character:
("aba", "baba")
("aba", "baba")
("aba", "baba")
("aba", "baba")
("aba", "baba")
("aba", "baba")
The underlined portions are the substrings that are chosen from s and t.

​​Example 2:

Input: s = "ab", t = "bb"
Output: 3
Explanation: The following are the pairs of substrings from s and t that differ by 1 character:
("ab", "bb")
("ab", "bb")
("ab", "bb")
​​​​The underlined portions are the substrings that are chosen from s and t.


&nbsp;
Constraints:


	1 &lt;= s.length, t.length &lt;= 100
	s and t consist of lowercase English letters only.


class Solution:
    def countSubstrings(self, s: str, t: str) -> int:
        res = 0
        for i in range(len(s)):
            for j in range(len(t)):
                miss, pos = 0, 0
                while i + pos < len(s) and j + pos < len(t) and miss < 2:
                    miss += s[i + pos] != t[j + pos]
                    res += miss == 1
                    pos += 1
        return res
// version 1 : O(mn) space
class Solution {
    public int countSubstrings(String s, String t) {
        int m = s.length(), n = t.length();

        int[][][] dp = new int[m][n][2];
        
        int res = 0;
        // first col s[0:i] match t[0:0]
        for (int i = 0; i < m; i++) {
            dp[i][0][0] = (s.charAt(i) == t.charAt(0)) ? 1 : 0;
            dp[i][0][1] = (s.charAt(i) == t.charAt(0)) ? 0 : 1;
            res += dp[i][0][1];
        }
      
        
        // first row s[0:0] match t[0:j]
        for (int j = 1; j < n; j++) {
            dp[0][j][0] = (s.charAt(0) == t.charAt(j)) ? 1 : 0;
            dp[0][j][1] = (s.charAt(0) == t.charAt(j)) ? 0 : 1;
            res += dp[0][j][1];
        }
        
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j][0] = (s.charAt(i) == t.charAt(j)) ? dp[i-1][j-1][0] + 1 : 0;
                dp[i][j][1] = (s.charAt(i) == t.charAt(j)) ? dp[i-1][j-1][1] : dp[i-1][j-1][0] + 1;
                res += dp[i][j][1];
            }
        }

        return res;
    }
}
class Solution {
public:
    int countSubstrings(string s, string t)
    {
        int n=s.size();
        int m=t.size();
        int ans=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int diff=0;
                for(int k=0;i+k<n&&j+k<m;k++)
                {
                    if(s[i+k]!=t[j+k])
                    {
                        diff++;
                    }
                    if(diff>1)
                    {
                        break;
                    }
                    ans+=diff;
                }
            }
        }
        return ans;
    }
};
var countSubstrings = function(s, t) {
    const count1 = countAllSubstr(s);
    const count2 = countAllSubstr(t);

    let res = 0;

    for (const [substr1, freq1] of count1) {
       for (const [substr2, freq2] of count2) {

           if (differByOneChar(substr1, substr2)) {
               res += freq1 * freq2;
           }
       }
    }

    return res;

    function countAllSubstr(str) {
        const n = str.length;
        const count = new Map();

        for (let i = 0; i < n; i++) {
            let substr = "";
            for (let j = i; j < n; j++) {
                substr += str.charAt(j);

                if (!count.has(substr)) count.set(substr, 0);
                count.set(substr, count.get(substr) + 1);
            }
        }

        return count;
    }

    function differByOneChar(str1, str2) {
        if (str1.length != str2.length) return false;

        const n = str1.length;

        let missed = 0;

        for (let i = 0; i < n; i++) {
            const char1 = str1.charAt(i);
            const char2 = str2.charAt(i);

            if (char1 != char2) missed++;

            if (missed > 1) return false;
        }

        return missed === 1;
    }

};
Count Substrings That Differ by One Character