-
Notifications
You must be signed in to change notification settings - Fork 33
/
Minimum_Window_Substring.java
71 lines (56 loc) · 2.56 KB
/
Minimum_Window_Substring.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
//Leetcode 76. Minimum Window Substring
//Question - https://leetcode.com/problems/minimum-window-substring/
class Solution {
public String minWindow(String s, String t) {
//creating a storage to check if the threshold frequency of each character in 't' is met.
HashMap<Character, Integer> freqTable = new HashMap<Character,Integer>();
//fill the frequency table
for(int i=0;i<t.length();i++){
char curr = t.charAt(i);
int freq = 1;
if(freqTable.containsKey(curr)){
freq = freqTable.get(curr);
freq++;
}
freqTable.put(curr,freq);
}
//initialize the window
int start = 0;
int end = 0;
String ans = "";
int windowLen = Integer.MAX_VALUE;
//keeps the count of distinct characters in 't' which are not yet in the window
int unmatchedChars = freqTable.size();
//start sliding the window
while(end<s.length()){
char endChar = s.charAt(end);
if(freqTable.containsKey(endChar)){
int freq = freqTable.get(endChar);
freq--;
freqTable.put(endChar, freq);
//minimum threshold reached for 'endChar'
if(freq==0) unmatchedChars--;
}
end++;
//as long as the threshold of all characters in 't' is maintained. Keep sliding the start of the window to the right. Trimming unnecessarcy characters to minimize window size.
//The sliding of start of the window to the right is triggered only when the threshold of all characters in 't' is met.
while(start<s.length() && unmatchedChars==0){
char startChar = s.charAt(start);
//update window length and answer after each slide.
if(end-start<windowLen){
windowLen = end-start;
ans = s.substring(start,end);
}
if(freqTable.containsKey(startChar)){
int freq = freqTable.get(startChar);
freq++;
freqTable.put(startChar,freq);
//threshold for startChar is not met. sliding the start of window has to be stopped.
if(freq>0) unmatchedChars++;
}
start++;
}
}
return ans;
}
}