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

题解:

类似快慢指针维护substring的方法在Minimum Window Substring里有总结.

随着runner向前推进找出最高frequency word的count. substring整个长度减掉最大的count 就是需要调换的字符数, 若大于k就移动walker.

Time Complexity: O(s.length()).

Space: O(1).

AC Java:

 1 class Solution {
 2     public int characterReplacement(String s, int k) {
 3         int [] map = new int[256];
 4         int walker = 0;
 5         int runner = 0;
 6         int maxCount = 0;
 7         int res = 0;
 8         while(runner < s.length()){
 9             maxCount = Math.max(maxCount, ++map[s.charAt(runner++)]);
10             while(runner-walker-maxCount > k){
11                 map[s.charAt(walker++)]--;
12             }
13
14             res = Math.max(res, runner-walker);
15         }
16         return res;
17     }
18 }

类似Longest Substring with At Most Two Distinct Characters.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/8412165.html

时间: 2024-07-31 12:52:38

LeetCode 424. Longest Repeating Character Replacement的相关文章

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

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】 Longest Substring Without Repeating Characters

题目: 给定一个字符串,返回该串没有重复字符的最长子串. 分析: 1)子串:子串要求是连续的. 2)无重复,出现重复就断了,必须从新的位置开始.而新的位置就是重复字符第一次出现位置的下一个位置. 3)整个串可能没有一处重复. 那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置.初始化为-1,表示都没有出现. 我们另外定义一个start 和end

Leetcode 解题 Longest Substring without repeating charcater python

原题: Given a string, find the length of the longest substring without repeating character For example, the Longest substring without repeating letters for "abcabcbb" is "abc", with the length is 3 思路:参考blog.csdn.net/hcbbt/article/detail

leetcode -day21 Longest Substring Without Repeating Characters

 1.Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the

[LeetCode] 5. Longest Substring Without Repeating Characters 最长回文子串

[LeetCode] 5. Longest Substring Without Repeating Characters Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: "babad" Output: "bab" Note: "aba&qu