zoukankan      html  css  js  c++  java
  • 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 = '';
     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     
  • 相关阅读:
    要想成为前端大神,那些你不得不知晓的web前端命名规范。
    正确开启Mockjs的三种姿势:入门参考(一)
    1024码农节-向自己致敬!
    ES6 常用总结(前端开发js技术进阶提升总结)
    JS快速构建数组方法
    React绑定事件动态化的实现方法
    JQ遇到$(‘.xxx’).attr(‘display’)一直返回undefined
    你所要掌握的最简单基础的React渲染优化
    MyBatis Generator
    Spring boot集成redis初体验
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6133692.html
Copyright © 2011-2022 走看看