日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

LeetCode 424. Longest Repeating Character Replacement

發布時間:2023/12/13 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode 424. Longest Repeating Character Replacement 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原題鏈接在這里:https://leetcode.com/problems/longest-repeating-character-replacement/description/

題目:

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 = 2Output: 4Explanation: Replace the two 'A's with two 'B's or vice versa.

Example 2:

Input: s = "AABABBA", k = 1Output: 4Explanation: Replace the one 'A' in the middle with 'B' and form "AABBBBA". The substring "BBBB" has the longest repeating letters, which is 4.

題解:

類似快慢指針維護substring的方法在Minimum Window Substring里有總結.

隨著runner向前推進找出最高frequency word的count. substring整個長度減掉最大的count 就是需要調換的字符數, 若大于k就移動walker.

Time Complexity: O(s.length()).

Space: O(1).

AC Java:

1 class Solution { 2 public int characterReplacement(String s, int k) { 3 int [] map = new int[256]; 4 int walker = 0; 5 int runner = 0; 6 int maxCount = 0; 7 int res = 0; 8 while(runner < s.length()){ 9 maxCount = Math.max(maxCount, ++map[s.charAt(runner++)]); 10 while(runner-walker-maxCount > k){ 11 map[s.charAt(walker++)]--; 12 } 13 14 res = Math.max(res, runner-walker); 15 } 16 return res; 17 } 18 }

類似Longest Substring with At Most Two Distinct Characters.

轉載于:https://www.cnblogs.com/Dylan-Java-NYC/p/8412165.html

總結

以上是生活随笔為你收集整理的LeetCode 424. Longest Repeating Character Replacement的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。