Skip to content

Commit a1ef6b6

Browse files
committed
sliding window
1 parent 0ff9a18 commit a1ef6b6

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
 (0)