如题:
给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次。在执行上述操作后,找到包含重复字母的最长子串的长度。
注意:字符串长度 和 k 不会超过 104。
int characterReplacement(char * s, int k){
int left = 0, right = 0; //左右指针
int lenth = strlen(s);
int max = 0;
int res = 0;
int number[26] = {0};//统计字母出现频率
while (right < lenth)
{
number[s[right] - 'A']++;//进入,频率++
max = fmax(max, number[s[right] - 'A']);
while (right - left + 1 > max + k)//如果长度超了,--
{
number[s[left++] - 'A']--;
}
res = fmax(res, right - left + 1);
right++;
}
return res;
}
本文介绍了一种解决字符串中寻找经过最多k次替换后得到的最长重复子串问题的算法。该算法通过双指针技术和计数数组实现,能够在限定时间内找到最优解。
861

被折叠的 条评论
为什么被折叠?



