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. ...
随机推荐
- 【Python算法】归纳、递归、归简
归简法(reduction) 指的是将某一问题转化成另一个问题,将一个未知问题归简成一个已解决的问题. 归纳法(induction) 首先要证明语句在某一基本情况下是成立的,然后证明他可以由一个对象推 ...
- 权限认证与授权(Shrio 框架)
权限概述 认证: 系统提供的用于识别用户身份的功能, 通常登录功能就是认证功能; -- 让系统知道你是谁 授权: 系统授予用户可以访问哪些功能的证书. -- 让系统知道你能做什么! 常见的权限控制方式 ...
- Ubutun使用记录——语系错误(转)
add by zhj: 对原文有修改,原文是在创建用户时出现的问题,而我是在使用psql时出现的, 但问题是相同的. 原文:http://www.douban.com/note/362250557/ ...
- MySQL单列索引和组合索引的区别介绍(转)
原文:http://database.51cto.com/art/201011/233234.htm MySQL单列索引是我们使用MySQL数据库中经常会见到的,MySQL单列索引和组合索引的区别可能 ...
- VC2005 warning C4819 消除方法
一. Warning C4819:The file contains a character that can ot be represented in the current code page(9 ...
- Linux vim编辑器常用命令
Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器 常用的vim命令如下图 补充: num+命令 对命令执行num次,如 5dd:剪切一行 * 5 即剪切5行,其它如此 /text ...
- 012-基于 git hooks 的前端代码质量控制解决方案
原文看这里:https://github.com/kuitos/kui...全部文章看这里 https://github.com/kuitos/kui... 国际惯例先说下故事背景 通常情况下,如果我 ...
- java-mybaits-00402-Mapper-动态sql-if、where、foreach、sql片段
1.动态sql(重点) 通过mybatis提供的各种标签方法实现动态拼接sql. 什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. ...
- 关于sails 初学者常见问题汇总
http://sailsdoc.swift.ren/ 这里有 sails中文文档 一.安装时: 先装nodejs,成功标志 node -v 安装sails 全局安装 node install sail ...
- Jmeter(七)Mongodb的增删改查
1.启动JMeter,新建线程组,设置线程组属性 2.右键添加-MongoDB Source Config 设置属性Server Address List:192.168.0.99 MongoDB S ...