-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumWindowSubstring.java
More file actions
301 lines (223 loc) · 9.86 KB
/
MinimumWindowSubstring.java
File metadata and controls
301 lines (223 loc) · 9.86 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
package Algorithms.SlidingWindow;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 28 Sept 2023
* @link 76. Minimum Window Substring <a href="https://leetcode.com/problems/minimum-window-substring/">LeetCode link</a>
* @description Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".
* @topics String, Hash Table, Sliding Window / Dynamic Sliding Window
* @companies Amazon, Google, Microsoft, Lyft, Bloomberg, LinkedIn, TikTok, SoFi, Snowflake, Snap, Zeta, Adobe, Walmart Labs, Uber, Airbnb, Yandex, Apple, Zopsmart, Salesforce, Apollo, Agoda, MakeMyTrip, thoughtspot, Nagarro
NOTE:
1. Here "s" should have all the chars in "t" ---> that means "s" can have more "t" chars i.e if it has {A=1, B=2} then "s" can have {A=11, B=3}
* This sum is similar to {@link Algorithms.SlidingWindow.LongestSubstringWithoutRepeatingCharacters}
*/
public class MinimumWindowSubstring {
public static void main(String[] args) {
String s = "ADOBECODEBANC";
String t = "ABC";
System.out.println("minWindow Using Resetting SlidingWindow (isMatch validation) => " + minWindowUsingResettingSlidingWindow(s, t));
System.out.println("minWindow using ASCII Array SlidingWindow => " + minWindowUsingAsciiArrSlidingWindow(s, t));
System.out.println("minWindow Using SlidingWindow => " + minWindowUsingSlidingWindow(s, t));
System.out.println("minWindow Using SlidingWindow Optimized => " + minWindowUsingSlidingWindowOptimized(s, t));
System.out.println("minWindow BruteForce => " + minWindowBruteForce(s, t));
}
/**
* WORKING but slower than above
* @TimeComplexity O(m*o), where m=s.length, n=t.length, o=tMap.size
* @SpaceComplexity O(m+n)
*
s="aaaaaaaaaaaabbbbbcddefg"
t="abcdd"
tMap - {a=1, b=1, c=1, d=2}
a a a a a a a a a a a a b b b b b c d d e f g
l r
*/
public static String minWindowUsingResettingSlidingWindow(String s, String t) { // UsingSlidingWindowBruteForceIsMatchValidation
int l = 0;
int m = s.length();
String res = "";
Map<Character, Integer> sMap = new HashMap<>();
Map<Character, Integer> tMap = new HashMap<>();
for(char c : t.toCharArray()) {
tMap.merge(c, 1, Integer::sum);
}
for(int r=0; r<m; r++) {
char c = s.charAt(r);
sMap.merge(c, 1, Integer::sum);
while(l<=r && isMatch(sMap, tMap)) {
c = s.charAt(l);
sMap.merge(c, -1, Integer::sum);
if(res.isEmpty() || res.length() > r-l+1) {
res = s.substring(l, r+1);
}
l++;
}
}
return res;
}
private static boolean isMatch(Map<Character, Integer> sMap, Map<Character, Integer> tMap) {
// return tMap.keySet().stream().allMatch(c-> tMap.get(c) <= sMap.getOrDefault(c,0));
for(char c : tMap.keySet()) {
if(sMap.getOrDefault(c, 0) < tMap.get(c)) {
return false;
}
}
return true;
}
/**
* @TimeComplexity O(m+n)
* @SpaceComplexity O(m+n)
*
* Same like {@link #minWindowUsingResettingSlidingWindow} but use "NEED" and "have" instead of isMatch()
*/
public static String minWindowUsingAsciiArrSlidingWindow(String s, String t) { // UsingAsciiArrSlidingWindow
int[] res = {0, -1};
int[] tCounter = new int['z'-'A'+1]; // 'A'=65, 'Z'=90, 'a'=97, 'z'=122 ---> 122-65+1 = 122-64 = 58
int[] sCounter = new int['z'-'A'+1];
for(char c: t.toCharArray()) {
tCounter[c-'A']++;
}
final int NEED = (int) Arrays.stream(tCounter).filter(x->x>0).count();
int have = 0; // do have++ only when c frequency in sMap.get(c) == tMap.get(c), and have-- when c in sMap.get(c) < tMap.get(c)
int l = 0;
for(int r=0; r<s.length(); r++) {
char c = s.charAt(r);
sCounter[c-'A']++;
if(tCounter[c-'A'] == sCounter[c-'A']) {
have++;
}
while(have == NEED) {
if(res[1]==-1 || res[1]-res[0]>r-l) {
res[0] = l;
res[1] = r;
}
c = s.charAt(l);
sCounter[c-'A']--;
if(tCounter[c-'A'] > sCounter[c-'A']) {
have--;
}
l++;
}
}
return s.substring(res[0], res[1]+1);
}
/**
* @TimeComplexity O(n+m)
* @SpaceComplexity O(m)
* same like {@link #minWindowUsingResettingSlidingWindow} but use "formed" and "REQUIRED" instead of isEqual()
* and instead of r-while-loop and then l-while-loop, use l-while-loop inside r-while-loop i.e., increment r by 1 and do l-while-loop
*/
public static String minWindowUsingSlidingWindow(String s, String t) {
if (s.isEmpty() || t.isEmpty() || s.length() < t.length()) {
return "";
}
Map<Character, Integer> tMap = new HashMap<>();
for (int i = 0; i < t.length(); i++) {
tMap.merge(t.charAt(i), 1, Integer::sum);
}
final int NEED = tMap.size();
int l = 0, r = 0;
int have = 0; // have++ only when c in sMap == tMap, and have-- when c in sMap < tMap
Map<Character, Integer> windowCounts = new HashMap<>(); // sMap
int[] ans = { -1, 0, 0 }; // [size, l, r]
while (r < s.length()) {
char c = s.charAt(r);
windowCounts.merge(c, 1, Integer::sum);
if (tMap.containsKey(c) && windowCounts.get(c).equals(tMap.get(c))) { // or tMap.getOrDefault(c, 0).equals(sMap.get(c))
have++;
}
while (l <= r && have == NEED) {
c = s.charAt(l);
if (ans[0]==-1 || r-l+1 < ans[0]) {
ans[0] = r-l+1;
ans[1] = l;
ans[2] = r;
}
windowCounts.merge(c, -1, Integer::sum);
if (tMap.containsKey(c) && windowCounts.get(c) < tMap.get(c) ) { // or tMap.getOrDefault(c, 0) > sMap.get(c)
have--;
}
l++;
}
r++;
}
return ans[0] == -1 ? "" : s.substring(ans[1], ans[2] + 1);
}
public static String minWindowUsingSlidingWindowOptimized(String s, String t) {
if (s.isEmpty() || t.isEmpty()) {
return "";
}
Map<Character, Integer> tMap = new HashMap<>();
for (int i = 0; i < t.length(); i++) {
tMap.merge(t.charAt(i), 1, Integer::sum);
}
int required = tMap.size();
List<Map.Entry<Integer, Character>> filteredS = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (tMap.containsKey(c)) {
filteredS.add(new AbstractMap.SimpleEntry<>(i, c));
}
}
int l = 0, r = 0, formed = 0;
Map<Character, Integer> windowCounts = new HashMap<>();
int[] ans = { -1, 0, 0 };
while (r < filteredS.size()) {
char c = filteredS.get(r).getValue();
windowCounts.merge(c, 1, Integer::sum);
if (tMap.containsKey(c) && windowCounts.get(c).intValue() == tMap.get(c).intValue()) {
formed++;
}
while (l <= r && formed == required) {
c = filteredS.get(l).getValue();
int end = filteredS.get(r).getKey();
int start = filteredS.get(l).getKey();
if (ans[0] == -1 || end - start + 1 < ans[0]) {
ans[0] = end - start + 1;
ans[1] = start;
ans[2] = end;
}
windowCounts.put(c, windowCounts.get(c) - 1);
if ( tMap.containsKey(c) && windowCounts.get(c) < tMap.get(c) ) {
formed--;
}
l++;
}
r++;
}
return ans[0] == -1 ? "" : s.substring(ans[1], ans[2] + 1);
}
/**
* @TimeComplexity - O(n^2)
* @Intution - the chars in t strings <= chars in subStr. So we can use 2 pointer sliding window technique
* @Approach - Check each and every subString and it's length >= t length
*/
public static String minWindowBruteForce(String s, String t) {
if(t.length() > s.length()) return "";
String subStr = "";
Map<String, Integer> tMap = Arrays.stream(t.split("")).collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e->1)) );
for(int i=0; i<s.length(); i++){
for(int j=i+t.length(); j<s.length()+1; j++){ // non exclusive
String tempSub = s.substring(i, j);
Map<String, Integer> subStrMap = Arrays.stream(tempSub.split("")).collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e->1)) );
// check t in tempSub
if(validate(tMap, subStrMap)){
if(subStr.length() > tempSub.length() || subStr.isEmpty())
subStr = tempSub;
}
}
}
return subStr;
}
static Boolean validate(Map<String, Integer> tMap, Map<String, Integer> subStrMap){
for(Map.Entry<String, Integer> te: tMap.entrySet()){
if(subStrMap.getOrDefault(te.getKey(), 0) < te.getValue())
return false;
}
tMap = new HashMap<>();
System.out.println(tMap);
return true;
}
}