-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.ts
34 lines (34 loc) · 1.03 KB
/
Solution.ts
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
function findSubstring(s: string, words: string[]): number[] {
const cnt: Map<string, number> = new Map();
for (const w of words) {
cnt.set(w, (cnt.get(w) || 0) + 1);
}
const ans: number[] = [];
const [m, n, k] = [s.length, words.length, words[0].length];
for (let i = 0; i < k; i++) {
let [l, r] = [i, i];
const cnt1: Map<string, number> = new Map();
while (r + k <= m) {
const t = s.substring(r, r + k);
r += k;
if (!cnt.has(t)) {
cnt1.clear();
l = r;
continue;
}
cnt1.set(t, (cnt1.get(t) || 0) + 1);
while (cnt1.get(t)! > cnt.get(t)!) {
const w = s.substring(l, l + k);
cnt1.set(w, cnt1.get(w)! - 1);
if (cnt1.get(w) === 0) {
cnt1.delete(w);
}
l += k;
}
if (r - l === n * k) {
ans.push(l);
}
}
}
return ans;
}