File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .leetcode ;
2+
3+ import java .util .HashMap ;
4+ import java .util .Map ;
5+
6+ public class Main424 {
7+ public int characterReplacement (String s , int k ) {
8+ int []map =new int [26 ];
9+ // 窗口内出现次数最多的字符的出现次数
10+ int maxCount =0 ;
11+ char []a =s .toCharArray ();
12+ int left =0 ,right =0 ;
13+ int res =0 ;
14+ // right一直向右扩充
15+ for (right =0 ;right <a .length ;right ++){
16+ map [a [right ]-'A' ]++;
17+ // 每扩充一格 就重新计算maxCount
18+ maxCount =Math .max (maxCount ,map [a [right ]-'A' ]);
19+
20+ // 重复字符可以填满整个窗口,更新结果。
21+ if (right -left +1 -maxCount <=k ){
22+ res =Math .max (right -left +1 ,res );
23+ }
24+ // 窗口大小 减去 重复字符出现次数>k ,说明重复字符不能填满整个窗口
25+ else {
26+ map [a [left ]-'A' ]--;
27+ left ++;
28+ }
29+ }
30+ return res ;
31+ }
32+
33+ public static void main (String [] args ) {
34+ Main424 m = new Main424 ();
35+ String s = "BBAAAB" ;
36+ int k = 3 ;
37+ System .out .println (m .characterReplacement (s , k ));
38+
39+ }
40+ }
You can’t perform that action at this time.
0 commit comments