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

  1. 求一个字符串转变成全部一样字符的字符串所需要的最小步骤数

length of the entire string - number of times of the maximum occurrin character in the string

  1. 最多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的更多相关文章

  1. [LeetCode] Longest Repeating Character Replacement 最长重复字符置换

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  2. Leetcode: Longest Repeating Character Replacement && G 面经

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  3. G 面经 && Leetcode: Longest Repeating Character Replacement

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  4. LeetCode 424. Longest Repeating Character Replacement

    原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/ 题目: Given ...

  5. 【LeetCode】424. 替换后的最长重复字符 Longest Repeating Character Replacement(Python)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,双指针,刷题群 目录 题目描述 题目大意 解题方法 双指针 代码 欢迎 ...

  6. 【leetcode】424. Longest Repeating Character Replacement

    题目如下: Given a string that consists of only uppercase English letters, you can replace any letter in ...

  7. [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  8. leetcode424 Longest Repeating Character Replacement

    """ Given a string s that consists of only uppercase English letters, you can perform ...

  9. 424 Longest Repeating Character Replacement 替换后的最长重复字符

    给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度.注意:字符串长度 和 k 不会超过 104. ...

随机推荐

  1. iOS 内存管理(转载)

     N久没维护这个博客了,从开始接触编程到现在已经三四年了.不太习惯写博客,这应该是个不好的习惯.所以从哪哪天开始,我得改变自己 (:       . 文采不太好,因此很多的文章都会借鉴他人的,但是我一 ...

  2. NSArray最简单的倒序

    NSArray里有 sortedArrayUsingSelector:等排序的方法,但是最简单的倒序排列的方法如下: NSArray *deArray = [[keyArrays reverseObj ...

  3. ArcGIS Server管理工具之批量发布动态地图服务工具.md

    友好阅读链接:(http://swj.me/2015/08/26/batchPublishtools/) update0918: 修复了创建链接文件时而出错的bug 修复了在24011的警告已处理的情 ...

  4. Mac 下,Redis(集群)的安装和配置

    1. Redis 安装步骤 1. 到github下载redis,我下载的是3.0.4 下载地址:GitHub 2. 将下载下来的redis-3.0.4.tar.gz拷贝到 /usr/local 目录下 ...

  5. leetcode 去除单链表倒数第k个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  6. 同步机制及windows同步函数的使用

    最近面试的许多公司都询问关于多线程的问题,但是问的深度一般不会很难,仅仅问相关的同步问题以及对应的API函数,下面是windows下几个常用的同步方法,对于应付帮助或者一般的开发都非常有用 目录一 临 ...

  7. 用 Python 实现每秒处理 120 万次 HTTP 请求

    用 Python 做到每秒处理上百万次 HTTP 请求,可能吗?也许不能,但直到最近,这已成为现实. 很多公司都在为了提升程序的执行性能和降低服务器的运营成本,而放弃 Python 去选择其它编程语言 ...

  8. 一种SPA(单页面应用)架构

    (如果对SPA概念不清楚的同学可以先自行了解相关概念) 平时喜欢做点小页面来玩玩,并且一直采用单页面应用(Single Page Application)的方式来进行开发.这种开发方式是在之前一年做的 ...

  9. strtoul函数的使用

    函数原型: unsigned long strtoul(const char *nptr,char **endptr,int base ) 参数1:字符串起始地址参数2:返回字符串有效数字的结束地址, ...

  10. flask内置session原理

    内置session原理 请求到来 当请求进来之后,先执行Flask对象的 __call__ 方法 def wsgi_app(self, environ, start_response): # 获取请求 ...