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
posted @ 2020-03-04 13:14  willwuss  阅读(147)  评论(0编辑  收藏  举报