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
    
  • 相关阅读:
    最全面的iOS和Mac开源项目和第三方库汇总
    15 个 Android 通用流行框架大全
    Android中常用的优秀开源框架
    iOS中拉伸图片的几种方式
    iOS View自定义窍门——UIButton实现上显示图片,下显示文字
    Java Queue的使用
    java用volatile或AtomicBoolean实现高效并发处理 (只初始化一次的功能要求)
    android开发音乐播放器--Genres和Art album的获取
    一个神奇的控件——Android CoordinatorLayout与Behavior使用指南
    在CodeBlocks 开发环境中配置使用OpenCV (ubuntu系统)
  • 原文地址:https://www.cnblogs.com/willwuss/p/12408901.html
Copyright © 2011-2022 走看看