-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSW.java
More file actions
456 lines (307 loc) · 11.2 KB
/
Copy pathSW.java
File metadata and controls
456 lines (307 loc) · 11.2 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Sliding Window
import java.util.*;
public class SW{
public static int longestSubStrWORepeat(String s){
int[] hash = new int[256]; //imp: we are using this hash array to store the last occurence of that element , not just the count
Arrays.fill(hash,-1);
int l = 0, r = 0, maxLen = 0;
while(r < s.length()){
if(hash[s.charAt(r)] != -1){
if(hash[s.charAt(r)] >= l){
l = hash[s.charAt(r)] + 1;
}
}
int len = r - l + 1;
maxLen = Math.max(len,maxLen);
hash[s.charAt(r)] = r; // since we are storing the last occurence hence we have equated it will 'r' and not 1 on R.H.S
r++;
}
return maxLen;
} // T = O(n) , S = O(256)
public static int maxZerosIIIbetter(int[] arr,int k){
int l = 0, zeros =0, maxLen = 0;
for(int r = 0;r < arr.length; r++){
if(arr[r] == 0){
zeros++;
}
while(zeros > k){
if(arr[l] == 0){
zeros--;
}
l++;
}
int len = r - l + 1;
maxLen = Math.max(maxLen,len);
}
return maxLen;
}
public static int maxZerosIIIoptimal(int[] arr, int k){
int l= 0, zeros = 0, maxLen = 0;
for(int r = 0; r < arr.length;r++){
if(arr[r] == 0){
zeros++;
}
if(zeros > k){
if(arr[l] == 0){
zeros--;
}
l++;
}
int len = r - l + 1;
maxLen = Math.max(maxLen,len);
}
return maxLen;
}
public static int maxFruitsInBasketBrute(int[] arr){ // T = O(N^2)
int max = 0;
for(int i = 0; i < arr.length;i++){
Set<Integer> basket = new HashSet<>();
int count = 0;
for(int j = i;j < arr.length;j++){
basket.add(arr[j]);
if(basket.size() > 2){
break;
}
count++;
}
max = Math.max(max,count);
}
return max;
}
public static int maxFruitsInBasketBetter(int[] arr){ // T = O(N + N)
Map<Integer, Integer> basket = new HashMap<>();
int l = 0;
int max = 0;
for(int r = 0;r < arr.length;r++){
basket.put(arr[r],basket.getOrDefault(arr[r],0) + 1);
while(basket.size() > 2){
basket.put(arr[l],basket.get(arr[l]) -1);
if(basket.get(arr[l]) == 0){
basket.remove(arr[l]);
}
l++;
}
max = Math.max(max, r - l + 1);
}
return max;
}
// public static int maxFruitsInBasketOptimal(int[] arr){ // not given on site see from video
// int l = 0, r = 0
// }
// https://www.geeksforgeeks.org/problems/max-sum-subarray-of-size-k5313/1
public static int maxSubarraySum(int[] arr, int k) { // k = windowSize
int windowSize = k;
int sum = 0;
int maxSum = Integer.MIN_VALUE;
// Sum of first window
for (int i = 0; i < windowSize; i++) {
sum = sum + arr[i];
}
maxSum = Integer.max(maxSum, sum);
int low = 0; // element to remove
int high = windowSize; // new element to add
while (high < arr.length) {
sum = sum - arr[low] + arr[high];
maxSum = Integer.max(maxSum, sum);
low++;
high++;
}
return maxSum;
}
// https://www.geeksforgeeks.org/problems/longest-k-unique-characters-substring0853/1
public static int longestKSubstr(String s, int k) {
int low = 0;
int longestStr = -1;
HashMap<Character, Integer> map = new HashMap<>();
for (int high = 0; high < s.length(); high++) {
char ch = s.charAt(high);
map.put(ch, map.getOrDefault(ch, 0) + 1);
// Shrink window if unique characters > k
while (map.size() > k) {
char leftChar = s.charAt(low);
map.put(leftChar, map.get(leftChar) - 1);
// Remove character if frequency becomes 0
if (map.get(leftChar) == 0) {
map.remove(leftChar);
}
low++;
}
// If exactly k unique characters
if (map.size() == k) {
int len = high - low + 1;
longestStr = Math.max(longestStr, len);
}
}
return longestStr;
}
public static int minSubArrayLen(int target, int[] arr) {
int minLen = Integer.MAX_VALUE;
int sum = 0;
int low = 0;
int high = 0;
while (high < arr.length) {
// add current element
sum = sum + arr[high];
// shrink window while valid
while (sum >= target) {
int length = high - low + 1;
minLen = Integer.min(minLen, length);
// remove left element
sum = sum - arr[low];
low++;
}
high++;
}
return minLen == Integer.MAX_VALUE ? 0 : minLen;
}
public static int lengthOfLongestSubstring(String s) {
int low = 0;
int maxLen = 0;
HashMap<Character, Integer> map = new HashMap<>();
for (int high = 0; high < s.length(); high++) {
char ch1 = s.charAt(high);
map.put(ch1, map.getOrDefault(ch1, 0) + 1);
int windowSize = high - low + 1;
// Shrink window if duplicates exist
while (windowSize > map.size()) {
char ch2 = s.charAt(low);
map.put(ch2, map.get(ch2) - 1);
if (map.get(ch2) == 0) {
map.remove(ch2);
}
low++;
windowSize = high - low + 1;
}
// Valid window (all unique characters)
if (windowSize == map.size()) {
int len = high - low + 1;
maxLen = Math.max(maxLen, len);
}
}
return maxLen;
}
// in the below approach we are using "Let me recalculate strongest character every time." intuition
public static int characterReplacement(String s, int k) { // T = O(26 * n) = O(n), S = O(255)
int low = 0;
int high;
int maxLen = Integer.MIN_VALUE;
int[] hashArr = new int[255]; // to calculate
for(high = 0;high < s.length();high++){
hashArr[s.charAt(high) - 'A'] += 1; // or hashArr[s.charAt(high)]++:
int windowSize = high - low + 1;
int maxEleFreq = maxFreqEle(hashArr);
int freq_of_non_max = windowSize - maxEleFreq;
while(freq_of_non_max > k){
hashArr[s.charAt(low) - 'A'] -= 1; // or hashArr[s.charAt(low)]--;
low++;
// recalculate after shrinking
windowSize = high - low + 1;
maxEleFreq = maxFreqEle(hashArr);
freq_of_non_max = windowSize - maxEleFreq;
}
if(freq_of_non_max <= k){
int len = high - low + 1;
maxLen = Integer.max(maxLen,len);
}
}
return maxLen;
}
private static int maxFreqEle(int[] arr){
int max = arr[0];
for(int i = 0;i < arr.length-1;i++){
if(arr[i+1]>max){
max = arr[i+1];
}
}
return max;
}
// we can slighlty optimize the above code by using "I already know strongest seen so far." intuition
public static int characterReplacement2(String s, int k) {
int low = 0;
int maxLen = 0;
int maxFreq = 0;
int[] freq = new int[26];
for (int high = 0; high < s.length(); high++) {
char ch = s.charAt(high);
freq[ch - 'A']++;
maxFreq = Math.max(maxFreq,
freq[ch - 'A']);
int windowSize = high - low + 1;
// invalid window
while ((windowSize - maxFreq) > k) {
freq[s.charAt(low) - 'A']--;
low++;
windowSize = high - low + 1;
}
maxLen = Math.max(maxLen,
high - low + 1);
}
return maxLen;
}
public static String minWindow(String s, String t) {
int[] hashArr = new int[128];
for (int i = 0; i < t.length(); i++) {
hashArr[t.charAt(i)]++;
}
int low = 0;
int count = t.length();
int minLen = Integer.MAX_VALUE;
int startIndex = -1;
for (int high = 0; high < s.length(); high++) {
char ch = s.charAt(high);
// needed character
if (hashArr[ch] > 0) {
count--;
}
hashArr[ch]--;
// valid window
while (count == 0) {
int windowSize = high - low + 1;
if (windowSize < minLen) {
minLen = windowSize;
startIndex = low;
}
char leftChar = s.charAt(low);
hashArr[leftChar]++;
// required char lost
if (hashArr[leftChar] > 0) {
count++;
}
low++;
}
}
return startIndex == -1
? ""
: s.substring(startIndex,
startIndex + minLen);
}
public static void main(String[] args) {
//String str = "cadbzabcd";
//System.out.println(longestSubStrWORepeat(str));
//int[] arr = {1,1,1,0,0,0,1,1,1,1,0};
//int k = 2;
//System.out.println(maxZerosIIIbetter(arr, k));
//System.out.println(maxZerosIIIoptimal(arr, k));
//int[] fruits = {3,3,3,1,2,1,1,2,3,3,4};
//System.out.println(maxFruitsInBasketBrute(fruits));
//System.out.println(maxFruitsInBasketBetter(fruits));
//int[] arr = {2, 1, 5, 1, 3, 2};
//int k = 3;
//System.out.println(maxSubarraySum(arr, k));
// int target = 7;
// int[] arr ={2,3,1,2,4,3};
// System.out.println(minSubArrayLen(target, arr));
// int k = 3;
// String s = "aabacbebebee";
// System.out.println("Longest substring length = " + longestKSubstr(s, k));
// String s = "abcabcbb";
// System.out.println("Length of Longest Substring Without Repeating Characters = " + lengthOfLongestSubstring(s));
// String s = "AABABBA";
// int k = 1;
// System.out.println("Longest substring length after replacement = " + characterReplacement(s, k));
// System.out.println("Longest substring length after replacement = " + characterReplacement2(s, k));
String s = "ADOBECODEBANC";
String t = "ABC";
System.out.println("Minimum window substring = " + minWindow(s, t));
}
}