-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProblem103.js
80 lines (69 loc) · 1.96 KB
/
Problem103.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
// Problem 103
//
// This problem was asked by Square.
//
// Given a string and a set of characters, return the shortest substring containing all the characters in the set.
//
// For example, given the string "figehaeci" and the set of characters {a, e, i}, you should return "aeci".
//
// If there is no substring containing all the characters in the set, return null.
//
// https://leetcode.com/problems/minimum-window-substring/
//
// O(S + T) Time complexity
// O(S + T) Space complexity
// S is the length of the string and T is the size of the set
/**
* Returns the shortest substring containing all characters in the set
* @param {string} s
* @param {Set<string>} set
* @return {string}
*/
function minWindow(s, set) {
if (s.length < set.size) return null;
// use two pointer technique, and keep a window
let rangeStart = -1;
let rangeEnd = -1;
let rangeLength = Number.MAX_SAFE_INTEGER;
const freqCount = new Map();
let start = 0;
let end = 0;
while (end < s.length) {
const char = s.charAt(end);
if (set.has(char)) {
// add to map
if (freqCount.has(char)) {
freqCount.set(char, freqCount.get(char) + 1);
} else {
freqCount.set(char, 1);
}
}
while (set.size === freqCount.size) {
const currLength = end - start + 1;
if (currLength < rangeLength) {
rangeLength = currLength;
rangeStart = start;
rangeEnd = end;
}
const startChar = s.charAt(start);
// remove count from startChar
if (freqCount.has(startChar)) {
const startCharCount = freqCount.get(startChar);
if (startCharCount === 1) freqCount.delete(startChar);
else {
freqCount.set(startChar, startCharCount - 1);
}
}
start++;
}
end++;
}
if (
rangeStart === -1 &&
rangeEnd === -1 &&
rangeLength === Number.MAX_SAFE_INTEGER
)
return null;
return s.substring(rangeStart, rangeEnd + 1);
}
export default minWindow;