-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLongestRepeatingCharacterReplacement.java
31 lines (29 loc) · 1.66 KB
/
LongestRepeatingCharacterReplacement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main.java.topcodingquestion.arraysandstrings;
import java.util.HashMap;
import java.util.Map;
//https://leetcode.com/problems/longest-repeating-character-replacement/
public class LongestRepeatingCharacterReplacement {
public int characterReplacement(String str, int k) {
if (str == null || str.length() == 0 || str.length() < k) return 0;
int windowStart = 0, maxLen = 0;
Map<Character, Integer> letterFrequencyMap = new HashMap<>();
int maxRepeatLetterCount = 0;
for (int windowEnd = 0; windowEnd < str.length(); windowEnd++) {
char rightChar = str.charAt(windowEnd);
letterFrequencyMap.put(rightChar, letterFrequencyMap.getOrDefault(rightChar, 0) + 1);
maxRepeatLetterCount = Math.max(maxRepeatLetterCount, letterFrequencyMap.get(rightChar));
// current window size is from windowStart to windowEnd, overall we have a letter which is
// repeating 'maxRepeatLetterCount' times, this means we can have a window which has one letter
// repeating 'maxRepeatLetterCount' times and the remaining letters we should replace.
// if the remaining letters are more than 'k', it is the time to shrink the window as we
// are not allowed to replace more than 'k' letters
if (windowEnd - windowStart + 1 - maxRepeatLetterCount > k) {
char leftChar = str.charAt(windowStart);
letterFrequencyMap.put(leftChar, letterFrequencyMap.getOrDefault(leftChar, 0) - 1);
windowStart++;
}
maxLen = Math.max(maxLen, windowEnd - windowStart + 1);
}
return maxLen;
}
}