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 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.

Solution:

Like 594. Longest Harmonious Subsequence, it is another typical problem of sliding window, two pointers: start and end, indicate a span in the string which qualify the descriptions.

In this problem, normally we will use a array whose size is 26, representing the appearances of each letters. The appearances of each letter will increase or decrease with the changing of sliding window.

I use a variable max_cnt to record the max appearances of a letter so far. There is an argument in leetcode discussion, some people believe that we should update the max_cnt after moving the start pointer forward.

It depends on what you want the max_cnt to represent.

  • If it represents the max appearances in the sliding window, it should change.
  • If it represents the max appearances by the ending pointers regardless the size of sliding window, it should not change.

In this problem, both solutions can pass OJ. The second get better efficiency, but I prefer to do not change. Since If we want to find a better solution, the max appearances should be greater, otherwise, just current one is optimal.

Time complexity is O(n).

This is the version of changing the max_cnt. It reports 29 ms in leetcode OJ system.

class Solution {
public:
    int characterReplacement(string s, int k) {
        // count the frequency of each letter
        int count[26] = {};
        int max_cnt = 0, max_len = 0;
        for (int start = 0, end = 0; end < s.size(); end++) {
            max_cnt = max(max_cnt, ++count[s[end] - ‘A‘]);
            while (end - start + 1 - max_cnt > k) {
                count[s[start] - ‘A‘]--;
                start++;
            }
            for(int i = 0 ; i < 26; i++){
                max_cnt = max(max_cnt, count[i]);
            }
            max_len = max(max_len, end - start + 1);
        }
        return max_len;
    }
};

This is the version of no changing. It reports the 9 ms in leetcode OJ system.

class Solution {
public:
    int characterReplacement(string s, int k) {
        int count[26] = {};
        int max_cnt = 0, max_len = 0;
        for (int start = 0, end = 0; end < s.size(); end++) {
            max_cnt = max(max_cnt, ++count[s[end] - ‘A‘]);
            while (end - start + 1 - max_cnt > k) {
                count[s[start] - ‘A‘]--;
                start++;
            }
            max_len = max(max_len, end - start + 1);
        }
        return max_len;
    }
};
时间: 2024-10-18 04:35:34

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

[滑动窗口] 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 begi

[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

[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

Longest Repeating Character Replacement

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

Longest Repeating Subsequence

Description Given a string, find length of the longest repeating subsequence such that the two subsequence don’t have same string character at same position, i.e., any ith character in the two subsequences shouldn’t have the same index in the origina

LeetCode 1156. Swap For Longest Repeated Character Substring

原题链接在这里:https://leetcode.com/problems/swap-for-longest-repeated-character-substring/ 题目: Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters. Example 1: In

[刷题] 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

G面经Prepare: Longest All One Substring

give a string, all 1 or 0, we can flip a 0 to 1, find the longest 1 substring after the flipping 这是一个简单版本of LC 424 Longest Repeating Character Replacement 又是Window, 又是Two Pointers Window还是采用每次都try to update left使得window valid, 每次都检查最大window 1 package