[滑动窗口] leetcode 424 Longest Repeating Character Replacement

problem:https://leetcode.com/problems/longest-repeating-character-replacement/

维护一个最多包含k个额外字符的滑动窗口。需要记录当前出现次数最多字符的出现次数来判断窗口是否合法,如果超过了,就把首指针向后挪一位,同时更新最多出现次数。对每个合法窗口,取其中的最大值。

class Solution {
public:
    int characterReplacement(string s, int k) {

        int begin = 0;
        int res = 0;
        int maxLen = 0;
        vector<int> count(26, 0);
        for(int i = 0;i < s.size();i++) // end
        {
            count[s[i] - ‘A‘]++;
            maxLen = max(maxLen, count[s[i] - ‘A‘]);
            int len = i - begin + 1;
            if(len > maxLen + k)
            {
                count[s[begin] - ‘A‘]--;
                begin++;
                maxLen = *max_element(count.begin(), count.end());
            }
       //     cout << res << " ";
            res = max(res, i - begin + 1);
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/fish1996/p/11337010.html

时间: 2024-08-04 22:24:11

[滑动窗口] leetcode 424 Longest Repeating Character Replacement的相关文章

LeetCode 424. Longest Repeating Character Replacement

原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/ 题目: 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 leng

424. Longest Repeating Character Replacement

Problem statement: 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

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

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 abo

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

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 abo

Longest Repeating Character Replacement

2018-06-29 22:56:24 问题描述: 问题求解: 本题是一条字符串问题,且是求Optimal,自然最初想到的是使用DP来进行求解,但是问题就是如果采用DP的话,前一个状态也太多了,和替换了多少了k值相关,因此从这个角度来说,使用DP来处理本题是不太合适的. 那么,另一个处理的手段滑动窗口就呼之欲出了. 在本题中窗口[start, end]来维护,其中只要窗口长度 - 窗口中出现字符最多的个数 <= k则当前窗口的长度符合题意,只要找出最长的符合条件的窗口即可. 问题是如果出现了上述

[滑动窗口] leetcode 1004 Max Consecutive Ones III

problem:https://leetcode.com/problems/max-consecutive-ones-iii/ 维护最多包含k个0的滑动窗口,一旦超过了k个0,把队首的0 pop出来.不断更新当前滑动窗口中的数据个数,并取最大值返回即可. class Solution { public: int longestOnes(vector<int>& A, int K) { int count = 0; int index = -1; deque<int> zer

滑动窗口 - leetcode

220. Contains Duplicate III //这里需要两个指针i和j,刚开始i和j都指向0,然后i开始向右走遍历数组,如果i和j之差大于k,且m中有nums[j],则删除并j加一.这样保证了m中所有的数的下标之差都不大于k,然后我们用map数据结构的lower_bound()函数来找一个特定范围,就是大于或等于nums[i] - t的地方,所有小于这个阈值的数和nums[i]的差的绝对值会大于t (可自行带数检验).然后检测后面的所有的数字,如果数的差的绝对值小于等于t,则返回tr

leetcode 3 Longest Substring Without Repeating Characters(滑动窗口)

用滑动窗口的思想来做.用一个unordered_map来查询之前的char有没有在现在的窗口中. class Solution { public: int lengthOfLongestSubstring(string s) { unordered_map<char,int>mp; int ans=0,now=0;//now is the window's instant length int b=0,e=0;//the window's begin and end int len=s.len

[刷题] LeetCode 3 Longest Substring Without Repeating Character

要求 在一个字符串中寻找没有重复字母的最长子串 思路 滑动窗口 如果当前窗口没有重复字母,j右移,直到包含重复字母 i右移,直到不包含重复字母 用数组记录字母是否出现过,判断重复 实现 1 class Solution{ 2 public: 3 int lenthOfLongestSubstring(string s){ 4 int freq[256] = {0}; 5 int l = 0, r = -1; 6 int res = 0; 7 8 while(l < s.size()){ 9 if