File tree 1 file changed +37
-0
lines changed
424.Longest Repeating Character Replacement
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int characterReplacement (String s , int k ) {
3
+
4
+ int [] freq = new int [26 ];
5
+ int left =0 ;
6
+ int maxFreq =0 ;
7
+ int maxWindow =0 ;
8
+
9
+ for (int right =0 ;right <s .length ();right ++){
10
+ //update the frequency of the current characters
11
+ freq [s .charAt (right )-'A' ]++;
12
+ //update thr max frequency
13
+ maxFreq =Math .max (maxFreq ,freq [s .charAt (right )-'A' ]);
14
+
15
+ int windowLength =right -left +1 ;
16
+ //if the windowLength-max frequency>k
17
+ //then we need to shrink the window
18
+
19
+ if (windowLength -maxFreq >k ){
20
+ freq [s .charAt (left )-'A' ]--;
21
+ left ++;
22
+
23
+ }
24
+
25
+ windowLength =right -left +1 ;
26
+ maxWindow =Math .max (maxWindow ,windowLength );
27
+
28
+
29
+
30
+ }
31
+
32
+ return maxWindow ;
33
+
34
+
35
+
36
+ }
37
+ }
You can’t perform that action at this time.
0 commit comments