Skip to content

Commit 0a92c98

Browse files
authored
Create 3008-find-beautiful-indices-in-the-given-array-ii.js
1 parent 99c1c16 commit 0a92c98

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} a
4+
* @param {string} b
5+
* @param {number} k
6+
* @return {number[]}
7+
*/
8+
var beautifulIndices = function (s, a, b, k) {
9+
let res = []
10+
let v1 = []
11+
let v2 = []
12+
getPatternMatchingIndex(s, a, v1)
13+
getPatternMatchingIndex(s, b, v2)
14+
for (let i = 0, j = 0; i < v1.length; i++) {
15+
while (j < v2.length && v1[i] > v2[j] && Math.abs(v1[i] - v2[j]) > k) {
16+
j++
17+
}
18+
if (j < v2.length && Math.abs(v1[i] - v2[j]) <= k) {
19+
res.push(v1[i])
20+
}
21+
}
22+
return res
23+
}
24+
25+
function getPatternMatchingIndex(s, a, v) {
26+
let t = a + '@' + s
27+
let lps = [0]
28+
for (let i = 1; i < t.length; i++) {
29+
let ind = lps[i - 1]
30+
while (ind > 0 && t[ind] !== t[i]) {
31+
ind = lps[ind - 1]
32+
}
33+
lps.push(t[ind] === t[i] ? ind + 1 : 0)
34+
}
35+
for (let i = 0; i < lps.length; i++) {
36+
if (lps[i] === a.length) {
37+
v.push(i - 2 * a.length)
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)