05 August 2008
给一个string和k,做k次操作改变字符,使某个子字符串均为重复的字符,最长
need to know how many letters in our substring that we need to replace. To find out the lettersToReplace = (end - start + 1) - mostFreqLetter;
Time Complexity: O(N)
Space Complexity: O(26) = O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int characterReplacement(String s, int k) {
int[] freq = new int[26];
int mostFreqLetter = 0;
int left = 0;
int max = 0;
for(int right = 0; right < s.length(); right++){
freq[s.charAt(right) - 'A']++;
mostFreqLetter = Math.max(mostFreqLetter, freq[s.charAt(right) - 'A']);
int lettersToChange = (right - left + 1) - mostFreqLetter;
if(lettersToChange > k){
freq[s.charAt(left) - 'A']--;
left++;
}
max = Math.max(max, right - left + 1);
}
return max;
}
}