Skip to content

Commit 81e72f6

Browse files
Update BooyerMoore.js
1 parent 8111a43 commit 81e72f6

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

String/BoyerMoore.js

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
*
33
*
44
*Implementation of the Boyer-Moore String Search Algo.
@@ -11,24 +11,24 @@
1111
*
1212
**/
1313
const buildBadMatchTable = (str) => {
14-
let tableObj = {};
15-
let strLength = str.length;
16-
for (var i = 0; i < strLength - 1; i++) {
17-
tableObj[str[i]] = strLength - 1 - i;
14+
const tableObj = {};
15+
const strLength = str.length;
16+
for (let i = 0; i < strLength - 1; i++) {
17+
tableObj[str[i]] = strLength - 1 - i;
1818
}
19-
if (tableObj[str[strLength-1]] == undefined) {
20-
tableObj[str[strLength-1]] = strLength;
19+
if (tableObj[str[strLength - 1]] === undefined) {
20+
tableObj[str[strLength - 1]] = strLength;
2121
}
2222
return tableObj;
2323
}
2424

2525

26-
let boyerMoore = (str, pattern)=> {
27-
var badMatchTable = buildBadMatchTable(pattern),
28-
offset = 0,
29-
patternLastIndex = pattern.length - 1,
30-
scanIndex = patternLastIndex,
31-
maxOffset = str.length - pattern.length;
26+
const boyerMoore = (str, pattern) => {
27+
let badMatchTable = buildBadMatchTable(pattern);
28+
let offset = 0;
29+
let patternLastIndex = pattern.length - 1;
30+
let scanIndex = patternLastIndex;
31+
let maxOffset = str.length - pattern.length;
3232
// if the offset is bigger than maxOffset, cannot be found
3333
while (offset <= maxOffset) {
3434
scanIndex = 0;
@@ -39,7 +39,7 @@ let boyerMoore = (str, pattern)=> {
3939
}
4040
scanIndex++;
4141
}
42-
var badMatchString = str[offset + patternLastIndex];
42+
const badMatchString = str[offset + patternLastIndex];
4343
if (badMatchTable[badMatchString]) {
4444
// increase the offset if it exists
4545
offset += badMatchTable[badMatchString]
@@ -49,10 +49,4 @@ let boyerMoore = (str, pattern)=> {
4949
}
5050
return -1;
5151
}
52-
export {boyerMoore}
53-
54-
55-
56-
57-
58-
52+
export { boyerMoore }

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy