-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path048-smallest-substring-containing-all-patterns.js
More file actions
127 lines (93 loc) · 3.03 KB
/
048-smallest-substring-containing-all-patterns.js
File metadata and controls
127 lines (93 loc) · 3.03 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
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'findSmallestSubstringWindow' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. STRING_ARRAY patterns
* 2. STRING S
*/
function findSmallestSubstringWindow(patterns, S) {
if (!S || !patterns || patterns.length === 0) {
return [-1, -1];
}
const uniquePatterns = [...new Set(patterns)];
const requiredPatterns = uniquePatterns.length;
const occurrences = [];
uniquePatterns.forEach((pattern, patternId) => {
const patternLen = pattern.length;
for (let i = 0; i <= S.length - patternLen; i++) {
if (S.substring(i, i + patternLen) === pattern) {
occurrences.push({
start: i,
end: i + patternLen - 1,
patternId: patternId
});
}
}
});
const patternsFound = new Set(occurrences.map(o => o.patternId));
if (patternsFound.size < requiredPatterns) {
return [-1, -1];
}
occurrences.sort((a, b) => a.start - b.start);
let minStart = -1;
let minEnd = -1;
let minLength = Infinity;
let left = 0;
const patternCount = new Map();
let covered = 0;
for (let right = 0; right < occurrences.length; right++) {
const patternId = occurrences[right].patternId;
const currentCount = patternCount.get(patternId) || 0;
patternCount.set(patternId, currentCount + 1);
if (patternCount.get(patternId) === 1) {
covered++;
}
while (covered === requiredPatterns && left <= right) {
const startIdx = occurrences[left].start;
const endIdx = occurrences[right].end;
const windowLength = endIdx - startIdx + 1;
if (windowLength < minLength) {
minLength = windowLength;
minStart = startIdx;
minEnd = endIdx;
}
const leftPatternId = occurrences[left].patternId;
patternCount.set(leftPatternId, patternCount.get(leftPatternId) - 1);
if (patternCount.get(leftPatternId) === 0) {
covered--;
}
left++;
}
}
if (minStart === -1) {
return [-1, -1];
}
return [minStart, minEnd];
}
function main() {
const patternsCount = parseInt(readLine().trim(), 10);
let patterns = [];
for (let i = 0; i < patternsCount; i++) {
const patternsItem = readLine();
patterns.push(patternsItem);
}
const S = readLine();
const result = findSmallestSubstringWindow(patterns, S);
process.stdout.write(result.join('\n') + '\n');
}