• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

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.

 

Prefered solution:

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

 

 

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: (Idea the same but easy to understand)

 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     

 

posted @ 2016-12-05 13:58  neverlandly  阅读(425)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3