File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
longest-repeating-character-replacement Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .HashMap ;
2
+ import java .util .Map ;
3
+
4
+ public class Solution {
5
+ public int characterReplacement (String s , int k ) {
6
+ int maxLen = 0 ;
7
+ int maxCount = 0 ;
8
+ Map <Character , Integer > counter = new HashMap <>();
9
+ int start = 0 ;
10
+
11
+ for (int end = 0 ; end < s .length (); end ++) {
12
+ char endChar = s .charAt (end );
13
+ counter .put (endChar , counter .getOrDefault (endChar , 0 ) + 1 );
14
+ maxCount = Math .max (maxCount , counter .get (endChar ));
15
+
16
+ while (end - start + 1 - maxCount > k ) {
17
+ char startChar = s .charAt (start );
18
+ counter .put (startChar , counter .get (startChar ) - 1 );
19
+ start ++;
20
+ }
21
+
22
+ maxLen = Math .max (maxLen , end - start + 1 );
23
+ }
24
+
25
+ return maxLen ;
26
+ }
27
+ }
28
+
29
+
You can’t perform that action at this time.
0 commit comments