Skip to content

Commit 95411ef

Browse files
authored
Update 3008-find-beautiful-indices-in-the-given-array-ii.js
1 parent ab956d2 commit 95411ef

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

3008-find-beautiful-indices-in-the-given-array-ii.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ var beautifulIndices = function (s, a, b, k) {
1212
const js = kmp(s, b)
1313
if (js.length === 0) return []
1414

15-
const answer = []
15+
const res = []
1616
let p = 0
1717
let q = 0
1818

1919
while (p < is.length && q < js.length) {
2020
const distance = Math.abs(is[p] - js[q])
2121

2222
if (distance <= k) {
23-
answer.push(is[p])
23+
res.push(is[p])
2424
p++
2525
} else if (is[p] < js[q]) {
2626
p++
@@ -29,47 +29,46 @@ var beautifulIndices = function (s, a, b, k) {
2929
}
3030
}
3131

32-
return answer
32+
return res
3333
}
3434

3535
function kmp(str1, str2) {
3636
const pattern = buildPattern(str2)
3737

38-
const answer = []
38+
const res = []
3939
let i = 0
4040
let j = 0
4141

4242
while (i < str1.length) {
4343
if (str1[i] === str2[j]) {
4444
i++
4545
j++
46-
4746
if (j === str2.length) {
48-
answer.push(i - str2.length)
49-
j = pattern[j - 1] + 1
47+
res.push(i - str2.length)
48+
j = pattern[j - 1]
5049
}
5150
} else if (j > 0) {
52-
j = pattern[j - 1] + 1
51+
j = pattern[j - 1]
5352
} else {
5453
i++
5554
}
5655
}
5756

58-
return answer
57+
return res
5958
}
6059

6160
function buildPattern(str) {
62-
const pattern = new Array(str.length).fill(-1)
61+
const pattern = new Array(str.length).fill(0)
6362
let i = 1
6463
let j = 0
6564

6665
while (i < str.length) {
6766
if (str[i] === str[j]) {
67+
j++
6868
pattern[i] = j
6969
i++
70-
j++
7170
} else if (j > 0) {
72-
j = pattern[j - 1] + 1
71+
j = pattern[j - 1]
7372
} else {
7473
i++
7574
}
@@ -78,6 +77,7 @@ function buildPattern(str) {
7877
return pattern
7978
}
8079

80+
8181
// another
8282

8383
/**

0 commit comments

Comments
 (0)