zoukankan      html  css  js  c++  java
  • Leetcode 424 Longest Repeating Character Replacement (替换k字符后最长重复字符数) (滑动窗口)

    Leetcode 424

    问题描述

    Given a string s that consists of only uppercase English letters, you can perform at most k operations on that string.
    
    In one operation, you can choose any character of the string and change it to any other uppercase English character.
    
    Find the length of the longest sub-string containing all repeating letters you can get after performing the above operations.
    

    例子

    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.
    

    方法一

      Java建立HashMap, Python使用字典.

    ** Solution Java **
    ** 20ms, beats 19.02% **
    ** 41.4MB, beats 10.00% **
    class Solution {
        public int characterReplacement(String s, int k) {
            if (s == null || s.length() == 0) 
                return 0;
            Map<Character, Integer> map = new HashMap<>();
            char[] S = s.toCharArray();
            int start = 0, maxCounter = 0, result = 0;
            for (int end = 0; end < S.length; ++end) {
                map.put(S[end], map.getOrDefault(S[end], 0) + 1);
                maxCounter = Math.max(maxCounter, map.get(S[end]));
                if (end - start + 1 - maxCounter > k) {
                    map.put(S[start], map.get(S[start]) - 1);
                    ++start;
                }
                result = Math.max(result, end - start + 1);
            }
            return result;
        }
    }
    
    ** Solution Python **
    ** 108ms, beats 83.04% **
    ** 12.0MB, beats 100,00% **
    class Solution:
        def characterReplacement(self, s, k):
            count = {}
            start, maxLen, res = 0, 0, 0
            for end in range(len(s)):
                count[s[end]] = count.get(s[end], 0) + 1
                maxLen = max(maxLen, count[s[end]])
                if (end - start + 1 - maxLen > k) :
                    count[s[start]] -= 1
                    start += 1
                res = max(res, end - start + 1)
            return res
    

    方法二

      用Array代替HashMap.
      Python3也可用collections.Counter()的方法,但速度过慢。

    ** Solution Java **
    ** 3ms, beats 99.63% **
    ** 39.2MB, beats 10.00% **
    class Solution{
        public int characterReplacement(String s, int k) {
            if (s == null || s.length() == 0)
                return 0;
            int[] count = new int[256];
            char[] S = s.toCharArray();
            int start = 0, res = 0, maxLength = 0;
            for (int end = 0; end < S.length; ++end) {
                maxLength = Math.max(maxLength, ++count[S[end]]);
                if (end - start + 1 - maxLength > k) 
                    --count[S[start++]];
                res = Math.max(res, end - start + 1);
            }
            return res;
        }
    }
    
    ** Solution Python3 **
    ** 108ms, 83.04% **
    ** 12.8MB, 100.00% **
    class Solution:
        def characterReplacement(self, s, k):
            count = [0 for _ in range(256)]
            start, maxLen, res = 0, 0, 0
            for end in range(len(s)):
                count[ord(s[end])] += 1
                maxLen = max(maxLen, count[ord(s[end])])
                if (end - start + 1 - maxLen > k) :
                    count[ord(s[start])] -= 1
                    start += 1
                res = max(res, end - start + 1)
            return res
    
  • 相关阅读:
    几个常见移动平台浏览器的User-Agent
    正则表达式那些事儿(三)
    正则表达式那些事儿(二)
    正则表达式那些事儿(一)
    jQuery官网plugins栏目下那些不错的插件
    UVA 11729 Commando War 突击战 【贪心】
    HDOJ 2084 数塔 【dp】
    HDOJ 1465 不容易系列之一 【错排公式 递推】
    HDOJ 2046 骨牌铺方格 【递推】
    HDOJ 2044 一只小蜜蜂... 【递推】
  • 原文地址:https://www.cnblogs.com/willwuss/p/12408901.html
Copyright © 2011-2022 走看看