Longest Repeating Character Replacement

2018-06-29 22:56:24

问题描述:

问题求解:

本题是一条字符串问题,且是求Optimal,自然最初想到的是使用DP来进行求解,但是问题就是如果采用DP的话,前一个状态也太多了,和替换了多少了k值相关,因此从这个角度来说,使用DP来处理本题是不太合适的。

那么,另一个处理的手段滑动窗口就呼之欲出了。

在本题中窗口[start, end]来维护,其中只要窗口长度 - 窗口中出现字符最多的个数 <= k则当前窗口的长度符合题意,只要找出最长的符合条件的窗口即可。

问题是如果出现了上述条件不满足的情况,那么就需要减小窗口大小使之满足条件。

这里有一个trick,就是在减小窗口大小的时候是不需要对maxcnt的大小进行修改的,因为如果要得到一个比当前的res更好的解,那么势必窗口大小要增大,同时maxcnt也要增大。因此这里不需要重新遍历一遍来获得新的窗口中的maxcnt。

    public int characterReplacement(String s, int k) {
        int res = 0;
        int start = 0;
        int[] cnt = new int[26];
        int maxcnt = 0;
        for (int end = 0; end < s.length(); end++) {
            maxcnt = Math.max(maxcnt, ++cnt[s.charAt(end) - ‘A‘]);
            if (end - start + 1 - maxcnt > k) {
                cnt[s.charAt(start++) - ‘A‘]--;
            }
            res = Math.max(res, end - start + 1);
        }
        return res;
    }

原文地址:https://www.cnblogs.com/TIMHY/p/9245773.html

时间: 2024-07-29 21:37:28

Longest Repeating Character Replacement的相关文章

[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

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

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

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