G 面经 && 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 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.

真是被这道题气死了,总是弄不对

The problem says that we can make at most k changes to the string (any character can be replaced with any other character). So, let's say there were no constraints like the k. Given a string convert it to a string with all same characters with minimal changes. The answer to this is

length of the entire string - number of times of the maximum occurring character in the string

Given this, we can apply the at most k changes constraint and maintain a sliding window such that

(length of substring - number of times of the maximum occurring character in the substring) <= k

 

The initial step is to extend the window to its limit, that is, the longest we can get to with maximum number of modifications. Until then the variable start will remain at 0.

Then as end increase, the whole substring from 0 to end will violate the rule, so we need to update start accordingly (slide the window). We move start to the right until the whole string satisfy the constraint again. Then each time we reach such situation, we update our max length.

 

 1 public class Solution {
 2     public int characterReplacement(String s, int k) {
 3         int[] count = new int[26];
 4         int res = 0;
 5         int start = 0;
 6         int maxCharCount = 0;
 7         char maxChar = '\0';
 8         for (int end=0; end<s.length(); end++) {
 9             char c = s.charAt(end);
10             count[c-'A']++;
11             if (maxCharCount < count[c-'A']) {
12                 maxCharCount = count[c-'A'];
13                 maxChar = c;
14             }
15             while (end-start+1-maxCharCount > k) {
16                 count[s.charAt(start)-'A']--;
17                 if (maxChar == s.charAt(start)) {
18                     maxCharCount--;
19                     for (int i=0; i<26; i++) {
20                         if (maxCharCount < count[i]) {
21                             maxCharCount = count[i];
22                             maxChar = (char)('A' + i);
23                         }
24                     }
25                 }
26                 start++;
27             }
28             res = Math.max(res, end-start+1);
29         }
30         return res;
31     }
32 }

 

Another Solution Window never shrink: 

 public int characterReplacement(String s, int k) {
        int[] charCount = new int[26];
        
        int left, right, maxCount, maxLen;
        left = right = maxCount = maxLen = 0;
    
        while(right < s.length()){
            charCount[s.charAt(right) - 'A']++;
            maxCount = Math.max(maxCount, charCount[s.charAt(right) - 'A']);
            if(right - left + 1 - maxCount > k) charCount[s.charAt(left++) - 'A']--;
            maxLen = Math.max(right++ - left + 1, maxLen);
        }
        return maxLen;
 }

  

Very easy and simple Python solution: (Preferred solution)

 1 import collections
 2 def findLongestSubstring(s,k):
 3     res=0
 4     left=0
 5     counts = collections.Counter()
 6     for right in range(0,len(s)):
 7         counts[s[right]] += 1
 8         char_w_maxCount = counts.most_common(1)[0][1]
 9         while right - left - char_w_maxCount + 1  > k:
10             counts[s[left]] -= 1
11             char_w_maxCount = counts.most_common(1)[0][1]
12             left +=1
13             
14         res = max(res,right-left+1)
15             
16     return res
17 
18     
19 print(findLongestSubstring("abab",2))                4
20 print(findLongestSubstring("aabbbba",1))        5
21 print(findLongestSubstring("abcaea",1))        3
22 print(findLongestSubstring("aababba",1))        4
23 print(findLongestSubstring("abcae",2))        4
24 print(findLongestSubstring("aaaaa",2))        5
25     

 

转载于:https://www.cnblogs.com/EdwardLiu/p/6133692.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值