LeetCode——Longest Repeating Character Replacement
1. Question
Given a string that consists of only uppercase English letters, you can replace any letter in the string with another letter at most k times. Find the length of a longest substring containing all repeating letters you can get after performing the above operations.
Note:
Both the string's length and k will not exceed 104.
Example 1:
Input:
s = "ABAB", k = 2
Output:
4
Explanation:
Replace the two 'A's with two 'B's or vice versa.
Example 2:
Input:
s = "AABABBA", k = 1
Output:
4
Explanation:
Replace the one 'A' in the middle with 'B' and form "AABBBBA".
The substring "BBBB" has the longest repeating letters, which is 4.
2. Solution
- 求一个字符串转变成全部一样字符的字符串所需要的最小步骤数
length of the entire string - number of times of the maximum occurrin character in the string
- 最多k次改变的限制,维持一个滑动窗口
(length of substring - number of times of the maximum occurring character in the substring) <= k
3. Code
class Solution {
public:
int characterReplacement(string s, int k) {
// sliding window
int len = s.length();
vector<int> vec(26, 0);
int maxValue = 0;
int start = 0;
int res = 0;
for (int end = 0; end < len; end++) {
vec[s[end] - 'A']++;
// 表示start~end之间出现次数最多的字符个数
maxValue = max(maxValue, vec[s[end] - 'A']);
// 滑动start,一直到满足条件
while (end - start + 1 - maxValue > k) {
vec[s[start] - 'A']--; // 滑掉的需要去掉
start++;
}
res = max(res, end - start + 1);
}
return res;
}
};
LeetCode——Longest Repeating Character Replacement的更多相关文章
- [LeetCode] Longest Repeating Character Replacement 最长重复字符置换
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- Leetcode: Longest Repeating Character Replacement && G 面经
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- G 面经 && Leetcode: Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- LeetCode 424. Longest Repeating Character Replacement
原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/ 题目: Given ...
- 【LeetCode】424. 替换后的最长重复字符 Longest Repeating Character Replacement(Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,双指针,刷题群 目录 题目描述 题目大意 解题方法 双指针 代码 欢迎 ...
- 【leetcode】424. Longest Repeating Character Replacement
题目如下: Given a string that consists of only uppercase English letters, you can replace any letter in ...
- [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- leetcode424 Longest Repeating Character Replacement
""" Given a string s that consists of only uppercase English letters, you can perform ...
- 424 Longest Repeating Character Replacement 替换后的最长重复字符
给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度.注意:字符串长度 和 k 不会超过 104. ...
随机推荐
- DocumentRoot \
w 有无\的区别. https://httpd.apache.org/docs/2.4/vhosts/examples.html hosts 127.0.0.1 w.w httpd.conf Ser ...
- PXE,ipmi,bare metal
IPMI(Intelligent Platform Management Interface)是一个智能平台管理接口. 用户可以利用IPMI 监视服务器等设备的物理特征,如各部件的温度.电压.风扇工作 ...
- js foreach
array1.forEach(callbackfn[, thisArg]) 参数 定义 array1 必需. 一个数组对象. callbackfn 必需. 一个接受最多三个参数的函数. 对于数组中的每 ...
- webservice接口问题:Payload: No message body writer has been found for class domain, ContentType: application/xml
当在使用cxf-rs的webservice的时候,有时候在传输数据,会发生这种错误 错误代码: Response-Code: 500 Content-Type: text/plain Headers: ...
- Android 查看自己的keystore的别名及相关信息
1.在DOS窗口下进入自己的keystore所在位置,输入 keytool -list -v -keystore xxxx.keystore -storepass 密码 xxxx.keystore是 ...
- 对Numpy数组按axis运算的理解
Python的Numpy数组运算中,有时会出现按axis进行运算的情况,如 >>> x = np.array([[1, 1], [2, 2]]) >>> x arr ...
- Object-Detection中常用的概念解析
常用的Region Proposal Selective Search Edge Boxes Softmax-loss softmax-loss层和softmax层计算大致是相同的,softmax是一 ...
- Error-The content of element type "web-app" must match "(icon?,display-
错误描述 The content of element type "web-app" must match "(icon?,display- name?,descript ...
- confluence安装、破解+MariaDB驱动、汉化
下载所需文件:http://fansik.edusaas.com/confluence/安装包:atlassian-confluence-5.8.10-x64.bin 汉化包:Confluence-5 ...
- STL make_heap push_heap pop_heap sort_heap
make_heap: default (1) template <class RandomAccessIterator> void make_heap (RandomAccessItera ...