-
Notifications
You must be signed in to change notification settings - Fork 90
/
Minimum_Window_Substring.js
131 lines (110 loc) · 3.13 KB
/
Minimum_Window_Substring.js
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
/*
Minimum Window Substring
https://leetcode.com/problems/minimum-window-substring/
Given a string S and a string T, find the minimum window in S which will contain all the characters in T
in complexity O(n).
Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
Note:
If there is no such window in S that covers all characters in T, return the empty string "".
If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
*/
// Solution 1
var minWindow = function (s, t) {
if (t.length === 0 || s.length < t.length) return "";
var start = 0;
var end = 0;
var solutionStart, solutionEnd;
var hashT = getHash(t);
var currentHash = {};
var currentCount = 0;
while (end < s.length) {
const letter = s.charAt(end);
if (hashT[letter]) {
currentHash[letter] = currentHash[letter] ? currentHash[letter] + 1 : 1;
if (currentHash[letter] <= hashT[letter]) currentCount++;
if (currentCount === t.length) {
while (
hashT[s[start]] === undefined ||
currentHash[s[start]] > hashT[s[start]]
) {
if (currentHash[s[start]] !== undefined)
currentHash[s[start]] = currentHash[s[start]] - 1;
start++;
}
if (
solutionEnd === undefined ||
end - start < solutionEnd - solutionStart
) {
solutionStart = start;
solutionEnd = end;
}
currentHash[s[start]] = currentHash[s[start]] - 1;
start++;
currentCount--;
}
}
end++;
}
return s.slice(solutionStart, solutionEnd + 1);
};
var getHash = function (t) {
var hash = {};
for (var i = 0; i < t.length; i++) {
const letter = t.charAt(i);
hash[letter] = hash[letter] ? hash[letter] + 1 : 1;
}
return hash;
};
// Solution 2
// Similar idea code slightly different;
var buildHash = function(t) {
let hash = {};
let occ = 0;
for(var i = 0; i < t.length; i++) {
occ = hash[t[i]] == undefined ? 0 : hash[t[i]];
hash[t[i]] = occ + 1;
}
return hash;
};
var minWindow2 = function(s, t) {
var hashT = buildHash(t);
var start = 0;
var end = 0;
var countLeft = t.length;
var minWindowLeft = -1;
var maxWindowRight = -1;
while(start < s.length && end < s.length) {
if(countLeft > 0) { // window does not contain all elements
if(hashT[s[end]] !== undefined) {
hashT[s[end]] = hashT[s[end]] - 1;
if(hashT[s[end]] >= 0) {
countLeft--;
}
}
if(countLeft > 0) {
end++;
}
} else { // found window
if(minWindowLeft == -1 || ((maxWindowRight - minWindowLeft + 1) > (end - start + 1)) ) {
minWindowLeft = start;
maxWindowRight = end;
}
if(hashT[s[start]] !== undefined) {
hashT[s[start]] = hashT[s[start]] + 1;
if(hashT[s[start]] > 0) {
countLeft++;
end++;
}
}
start++;
}
}
if(minWindowLeft > -1) {
return s.substring(minWindowLeft, maxWindowRight + 1);
}
return "";
};
module.exports.minWindow = minWindow;
module.exports.minWindow2 = minWindow2;