longest-repeating-character-replacement(难)

用sliding window的方法,之前还有个k不同元素好像也是类似的思路。有时间可以去复习下。

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

// sliding window

public class Solution {
    public int characterReplacement(String s, int k) {

        int[] count = new int[26];

        int maxch = -1; // the char with max count in current substr
        int max = 0; // the max count for single char in current substr
        int len = 0; // current substr length
        int ret = 0; // final result

        for (int i=0; i<s.length(); i++) {
            int tmp = s.charAt(i) - ‘A‘;
            count[tmp]++;
            len++;

            if (maxch == tmp) {
                max++;
            }
            else {
                if (count[tmp] > max) {
                    max = count[tmp];
                    maxch = tmp;
                }
            }

            if (len - max <= k) {
                if (len > ret) {
                    ret = len;
                }
            }

            while (len - max > k) {
                int newTmp = s.charAt(i-len+1) - ‘A‘;
                count[newTmp]--;
                len--;
                if (maxch == newTmp) {
                    max--;
                    for (int j=0; j<26; j++) {
                        if (count[j] > max) {
                            max = count[j];
                            maxch = j;
                        }
                    }
                }
            }
        }
        return ret;
    }
}
时间: 2024-10-08 20:36:44

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

Longest Repeating Character Replacement

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

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