-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathMinWindowSubstring.js
113 lines (90 loc) · 2.21 KB
/
MinWindowSubstring.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
/* 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"
*/
const minWindow = function(string, target) {
let substr = "";
let minLength = Infinity;
let ptr = 0;
for (let i = 1; i <= string.length; i++) {
let str = string.slice(ptr, i);
if (has(str, target) && str.length < minLength) {
minLength = str.length;
substr = str;
while(!target.includes(string[i]) && i<string.length-1) {
i++;
}
ptr = i;
}
}
return substr;
};
function has(s, t) {
const lookup = {};
//create a lookup for string
for(let i =0; i<s.length; i++) {
if(!lookup[s[i]]) lookup[s[i]] = true;
}
for(let j =0; j<t.length; j++) {
if(!lookup[t[j]]) return false;
}
return true;
}
let input = 'ADOBECODEBANC';
let target = 'ABC';
// console.log(minWindow(input, target));
console.log(minWindow("AAAAAB", "AB"));
console.log(minWindow("A", "AA"));
/*
var minWindow = function(s, t) {
// sliding window, from soln
if (t.length == 0) return ''
let [ left, right ] = [0, 0]
const wordMap = (str) => {
const map = {}
for (let c of str) {
if (map[c]) map[c] += 1
else map[c] = 1
}
return map
}
const tMap = wordMap(t)
let tMapCopy = Object.assign({}, tMap)
let keysCount = 0
let min = null
while (left < s.length && right < s.length) {
// initial window
if (s[right] in tMap) {
tMapCopy[s[right]] -= 1
if (tMapCopy[s[right]] == 0) keysCount += 1
}
if (keysCount >= Object.keys(tMap).length) {
// ideal sliding window found
// start sliding left until min(i)
while (left < right) {
let key = s[left]
if (tMapCopy[key] == 0) {
// characters with negative count
// were more (in duplicates)
break
} else {
// negative
tMapCopy[key] += 1
}
left += 1
}
if (
min == null
|| min[1] - min[0] > right - left
) {
min = [left, right]
}
}
right += 1
}
if (min !== null) return s.substr(min[0], min[1] - min[0] + 1)
return ""
};
*/