You are given two lowercase alphabet strings s and t. Return the number of pairs of substrings across s and t such that if we replace a single character to a different character, it becomes a substring of t.
Constraints
0 ≤ n ≤ 100 where n is the length of s
0 ≤ m ≤ 100 where m is the length of t
Example 1
Input
s = "ab"
t = "db"
Output
4
Explanation
We can have the following substrings:
"a" changed to "d"
"a" changed to "b"
"b" changed to "d"
"ab" changed to "db"
```
## 前置知识
- 动态规划
## 暴力法
### 思路
暴力的做法是枚举所有的子串 s[i:i+k] 和 s[j:j+k],其中 0 <= i < m - k, 0 <= j < n - k, 其中 m 和 n 分别为 s 和 t 的长度。
代码上可通过两层循环固定 i 和 j,再使用一层循环确定 k,k 从 0 开始计算。
如果子串不相同的字符:
- 个数为 0 ,则继续寻找。
- 个数为 1, 我们找到了一个可行的解,计数器 + 1
- 个数大于 1,直接 break,寻找下一个子串
最后返回计数器的值即可。
### 代码
代码支持:Python3
Python3 Code:
```py
classSolution:
defsolve(self,s,t):
ans=0
foriinrange(len(s)):
forjinrange(len(t)):
mismatches=0
forkinrange(min(len(s)-i,len(t)-j)):
mismatches+=s[i+k]!=t[j+k]
ifmismatches==1:
ans+=1
elifmismatches>1:
break
returnans
```
**复杂度分析**
令 n 为 s 长度,m 为 t 长度。
- 时间复杂度:$O(m \times n \times min(m,n))$
- 空间复杂度:$O(1)$
## 动态规划
### 思路
实际上,我们也可通过空间换时间的方式。先对数据进行预处理,然后使用动态规划来求解。
具体来说,我们可以定义二维矩阵 prefix, prefix[i][j] 表示以 s[i] 和 t[j] 结尾的最长前缀的长度(注意我这里的前缀不一定从 s 和 t 的第一个字符开始算)。比如 s = 'qbba', t = 'abbd', 那么 prefix[3][3] 就等于 bb 的长度,也就是 2。