<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 Tri:
    def __init__(self):
        self._children = {}

    def add(self, s: str):
        if s == "":
            if "end" not in self._children:
                self._children["end"] = 0
            self._children["end"] += 1
            return

        if s[0] not in self._children:
            self._children[s[0]] = Tri()

        self._children[s[0]].add(s[1:])

    def get(self, s: str) -> int:
        if s == "":
            if "end" in self._children:
                return self._children["end"]
            
            return 0
        elif s[0].upper() == s[0]:
            contains = 0
            for c in self._children:
                if c != "end" and c != s[0].lower():
                    contains += self._children[c].get(s[1:])
            return contains
        elif s[0] not in self._children:
            return 0

        return self._children[s[0]].get(s[1:])

class Solution:
    def countSubstrings(self, s: str, t: str) -> int:
        """
        We are given two substrings s and t and have to count the number of substrings of s
        that differ from a substring in t by one character
        We might want to use a tri to solve this problem
        We can create a tri for substrings in t
        Then we can iterate through each substring in a and for each substring, we can check if
        the substring is 1 different from a substring in t by using t's tri
        """
        tri = Tri()
        for i in range(len(t)):
            for j in range(i, len(t)):
                tri.add(t[i:j + 1])
        
        ans = 0
        for i in range(len(s)):
            for j in range(i, len(s)):
                differs_by_one = 0
                for k in range(i, j + 1):
                    differs_by_one += tri.get(s[i:k] + s[k].upper() + s[k + 1:j + 1])
                        
                ans += differs_by_one

        return ans


758