-
Notifications
You must be signed in to change notification settings - Fork 43
/
LongestRepeatingCharacterReplacement.java
316 lines (269 loc) · 9.98 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package LeetCodeJava.HashTable;
// https://leetcode.com/problems/longest-repeating-character-replacement/
import java.util.HashSet;
import java.util.HashMap;
import java.util.Map;
public class LongestRepeatingCharacterReplacement {
// V0
// IDEA : TWO POINTER + HASHMAP + FOR LOOP
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Hash_table/longest-repeating-character-replacement.py
public int characterReplacement(String s, int k) {
Map<Character, Integer> table = new HashMap<>();
int res = 0;
int p1 = 0;
for (int p2 = 0; p2 < s.length(); p2++) {
char c = s.charAt(p2);
table.put(c, table.getOrDefault(c, 0) + 1);
while ( (p2 - p1 + 1) - getMaxValue(table) > k) {
char c1 = s.charAt(p1);
table.put(c1, table.get(c1) - 1);
p1++;
}
res = Math.max(res, p2 - p1 + 1);
}
return res;
}
private int getMaxValue(Map<Character, Integer> map) {
int max = 0;
for (int value : map.values()) {
max = Math.max(max, value);
}
return max;
}
// V0'
public int characterReplacement_0_1(String s, int k) {
if (s.length() < k){
return 0;
}
Map<String, Integer> map = new HashMap<>();
String[] s_array = s.split("");
int l = 0;
int res = 0;
for (int r = 0; r < s_array.length; r++){
String key = s_array[r];
/**
* NOTE !!!
*
* we use hashmap for recording element, and its count (and keep updating it in for loop)
* e.g.
* key : element
* value : element count
*/
// NOTE : map.getOrDefault(key,0) syntax : if can find key, return its value, else, return default 0
map.put(key, map.getOrDefault(key,0)+1);
/** NOTE !!!
*
* put "(r - l + 1) - getMaxValue_0_1(map) > k" in while loop
* -> keep checking if "remaining different elements count < k"
* -> e.g. keep if they can be changed into the same element <= k times
*/
while ((r - l + 1) - getMaxValue_0_1(map) > k){
map.put(s_array[l], map.get(s_array[l])-1);
// map return key if val is 0
if (map.get(s_array[l]) == 0){
map.remove(s_array[l]);
}
l += 1;
}
res = Math.max(res, r - l + 1);
}
return res;
}
private int getMaxValue_0_1(Map<String, Integer> map) {
int max = 0;
for (int value : map.values()) {
max = Math.max(max, value);
}
return max;
}
// V0''
// IDEA : TWO POINTER + HASHMAP (modified by GPT)
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Hash_table/longest-repeating-character-replacement.py
public int characterReplacement_0(String s, int k) {
Map<Character, Integer> table = new HashMap<>();
int res = 0;
int p1 = 0, p2 = 0;
while (p2 < s.length()) {
char c = s.charAt(p2);
table.put(c, table.getOrDefault(c, 0) + 1);
p2++;
while (p2 - p1 - getMaxValue_0(table) > k) {
char c1 = s.charAt(p1);
table.put(c1, table.get(c1) - 1);
p1++;
}
res = Math.max(res, p2 - p1);
}
return res;
}
private int getMaxValue_0(Map<Character, Integer> map) {
int max = 0;
for (int value : map.values()) {
max = Math.max(max, value);
}
return max;
}
// // V0
// // IDEA : SLIDING WINDOW
// public int characterReplacement(String s, int k) {
//
// if (s.length() == 0 || s.equals(null)){
// return 0;
// }
//
// int ans = 0;
// char[] s_char = s.toCharArray();
//
// for (int i = 0; i < s_char.length-1; i++){
//
// Set set = new HashSet<>();
// int j = i;
// int tmp_k = k;
//
// while (tmp_k >= 0 && j < s_char.length){
// String cur = String.valueOf(s_char[j]);
// if (set.isEmpty()){
// set.add(cur);
// }else if(set.contains(cur)){
// j += 1;
// ans = Math.max(ans, j - i + 1);
// }else {
// if (k > 0){
// j += 1;
// k -= 1;
// ans = Math.max(ans, j - i + 1);
// }else{
// ans = Math.max(ans, j - i + 1);
// break;
// }
// }
// }
// }
//
// return ans;
// }
// V1
// IDEA : Sliding Window + Binary Search
// https://leetcode.com/problems/longest-repeating-character-replacement/editorial/
public int characterReplacement_2(String s, int k) {
// binary search over the length of substring
// lo contains the valid value, and hi contains the
// invalid value
int lo = 1;
int hi = s.length() + 1;
while (lo + 1 < hi) {
int mid = lo + (hi - lo) / 2;
// can we make a valid substring of length `mid`?
if (canMakeValidSubstring(s, mid, k)) {
// explore the right half
lo = mid;
} else {
// explore the left half
hi = mid;
}
}
// length of the longest substring that satisfies
// the given condition
return lo;
}
private Boolean canMakeValidSubstring(
String s,
int substringLength,
int k) {
// take a window of length `substringLength` on the given
// string, and move it from left to right. If this window
// satisfies the condition of a valid string, then we return
// true
int[] freqMap = new int[26];
int maxFrequency = 0;
int start = 0;
for (int end = 0; end < s.length(); end += 1) {
freqMap[s.charAt(end) - 'A'] += 1;
// if the window [start, end] exceeds substringLength
// then move the start pointer one step toward right
if (end + 1 - start > substringLength) {
// before moving the pointer toward right, decrease
// the frequency of the corresponding character
freqMap[s.charAt(start) - 'A'] -= 1;
start += 1;
}
// record the maximum frequency seen so far
maxFrequency = Math.max(maxFrequency, freqMap[s.charAt(end) - 'A']);
if (substringLength - maxFrequency <= k) {
return true;
}
}
// we didn't a valid substring of the given size
return false;
}
// V2
// IDEA : Sliding Window (Slow)
// https://leetcode.com/problems/longest-repeating-character-replacement/editorial/
public int characterReplacement_4(String s, int k) {
HashSet<Character> allLetters = new HashSet();
// collect all unique letters
for (int i = 0; i < s.length(); i++) {
allLetters.add(s.charAt(i));
}
int maxLength = 0;
for (Character letter : allLetters) {
int start = 0;
int count = 0;
// initialize a sliding window for each unique letter
for (int end = 0; end < s.length(); end += 1) {
if (s.charAt(end) == letter) {
// if the letter matches, increase the count
count += 1;
}
// bring start forward until the window is valid again
while (!isWindowValid(start, end, count, k)) {
if (s.charAt(start) == letter) {
// if the letter matches, decrease the count
count -= 1;
}
start += 1;
}
// at this point the window is valid, update maxLength
maxLength = Math.max(maxLength, end + 1 - start);
}
}
return maxLength;
}
private Boolean isWindowValid(int start, int end, int count, int k) {
// end + 1 - start - count is different element count
return end + 1 - start - count <= k;
}
// V3
// IDEA : Sliding Window (Fast)
// https://leetcode.com/problems/longest-repeating-character-replacement/editorial/
public int characterReplacement_5(String s, int k) {
int start = 0;
int[] frequencyMap = new int[26];
int maxFrequency = 0;
int longestSubstringLength = 0;
for (int end = 0; end < s.length(); end += 1) {
// if 'A' is 0, then what is the relative order
// or offset of the current character entering the window
// 0 is 'A', 1 is 'B' and so on
int currentChar = s.charAt(end) - 'A';
frequencyMap[currentChar] += 1;
// the maximum frequency we have seen in any window yet
maxFrequency = Math.max(maxFrequency, frequencyMap[currentChar]);
// move the start pointer towards right if the current
// window is invalid
Boolean isValid = (end + 1 - start - maxFrequency <= k);
if (!isValid) {
// offset of the character moving out of the window
int outgoingChar = s.charAt(start) - 'A';
// decrease its frequency
frequencyMap[outgoingChar] -= 1;
// move the start pointer forward
start += 1;
}
// the window is valid at this point, note down the length
// size of the window never decreases
longestSubstringLength = end + 1 - start;
}
return longestSubstringLength;
}
}