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. find-if-an-item-is-in-a-javascript-array

    http://stackoverflow.com/questions/143847/best-way-to-find-if-an-item-is-in-a-javascript-array Best ...

  2. glibc-2.23_malloc_consolidate_浅析

  3. Hive简介及使用

    一.Hive简介 1.hive概述 Apache Hive™数据仓库软件有助于使用SQL读取,编写和管理驻留在分布式存储中的大型数据集. 可以将结构投影到已存储的数据中.提供了命令行工具和JDBC驱动 ...

  4. x86架构下的控制寄存器CR0-CR4

    关于这几个寄存器,每次翻看intel手册都很不好找,干脆直接贴在这里吧!

  5. doxygen的简单使用(快速上手)

    在网上找了很久一个简单的doxygen教程,这个是最简单的,让你看完之后马上就能写doxygen格式的代码 doxygen是一种从源代码生成文档的工具,支持多种语言.当然,源代码中需按一定的格式写注释 ...

  6. Navicat连接mysql8出现1251错误

    我的博客:www.yuehan.online   因为加密方式的问题,在使用mysql8.0的时候需要修改加密规则才能连接navicat. 打开cmd,输入以下命令: ALTER USER 'root ...

  7. 使用selenium找出外卖点餐次数最多的10个顾客

    大锅在做外卖,给我说能否统计出这半年点餐次数最多的10个顾客,我不知道APP本身是否有这个功能,想了下最近用selenium较多,就用selenium尝试下吧. 1 定义一个类,这里先描述需要的属性和 ...

  8. LVS-DR 配置测试

    LVS Lvs体系结构 Lvs工作模式(3种) NAT-网络地址转换模式 当用户请求到达调度器时,调度器将请求报文的目标地址(即虚拟IP地址)改写成选定的Real Server地址,同时报文的目标端口 ...

  9. .globl分析

    Uboot中常看到.globl .globl _start _start: b reset .align .globl _TEXT_BASE _TEXT_BASE: .globl _start  /* ...

  10. 汇编文件后缀.s与.S

    转载:http://www.cnblogs.com/IamEasy_Man/archive/2011/08/10/2134212.html 一.大小写后缀的区别: .s:  汇编语言源程序;汇编 .S ...