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
    
  • 相关阅读:
    Mysql 配置主从
    ZJ 虚拟机扩直接扩原磁盘
    Linux 配置samba
    mysql 5.6 升级5.7
    binlog作用
    删除全部binlog不影响数据库运行,类似Oracle的archivelog
    mysql清理binlog
    Perl计数器
    perl增量分析日志
    perl 获取更新部分日志
  • 原文地址:https://www.cnblogs.com/willwuss/p/12408901.html
Copyright © 2011-2022 走看看