题目:
从S和T中任选两个等长的子字符串,这两个子字符串除了某一个字符不相等,其他位置的字符对应相等,请问有多少个组合
思路:
因为数据量较小,则暴力枚举
枚举i为s串的起点,j为t串的起点,k为子串的长度
如果字符不相同则cnt++
如果cnt=1则记录子串个数,如果cnt>1,说明不满足题目条件,跳出循环
class Solution {
public int countSubstrings(String s, String t) {
int n1=s.length(),n2=t.length();
int res=0;
for(int i=0;i<n1;i++)
for(int j=0;j<n2;j++)
{
int cnt=0;
for(int k=0;i+k<n1&&j+k<n2;k++)
{
if(s.charAt(i+k)!=t.charAt(j+k)) cnt++;
if(cnt>1) break;
if(cnt==1) res++;
}
}
return res;
}
}