Skip to content

Commit 7881cb5

Browse files
ayDavidGitHeregithub-actions
andauthored
Add Boyer-Moore string search algorithm (TheAlgorithms#990)
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent c81db62 commit 7881cb5

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@
295295
## String
296296
* [AlphaNumericPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/AlphaNumericPalindrome.js)
297297
* [AlternativeStringArrange](https://github.com/TheAlgorithms/Javascript/blob/master/String/AlternativeStringArrange.js)
298+
* [BoyerMoore](https://github.com/TheAlgorithms/Javascript/blob/master/String/BoyerMoore.js)
298299
* [CheckAnagram](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckAnagram.js)
299300
* [CheckCamelCase](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckCamelCase.js)
300301
* [CheckExceeding](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckExceeding.js)

String/BoyerMoore.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
*
3+
*
4+
*Implementation of the Boyer-Moore String Search Algorithm.
5+
*The Boyer–Moore string search algorithm allows linear time in
6+
*search by skipping indices when searching inside a string for a pattern.
7+
*
8+
*
9+
*
10+
*
11+
**/
12+
const buildBadMatchTable = (str) => {
13+
const tableObj = {}
14+
const strLength = str.length
15+
for (let i = 0; i < strLength - 1; i++) {
16+
tableObj[str[i]] = strLength - 1 - i
17+
}
18+
if (tableObj[str[strLength - 1]] === undefined) {
19+
tableObj[str[strLength - 1]] = strLength
20+
}
21+
return tableObj
22+
}
23+
24+
const boyerMoore = (str, pattern) => {
25+
const badMatchTable = buildBadMatchTable(pattern)
26+
let offset = 0
27+
const patternLastIndex = pattern.length - 1
28+
const maxOffset = str.length - pattern.length
29+
// if the offset is bigger than maxOffset, cannot be found
30+
while (offset <= maxOffset) {
31+
let scanIndex = 0
32+
while (pattern[scanIndex] === str[scanIndex + offset]) {
33+
if (scanIndex === patternLastIndex) {
34+
// found at this index
35+
return offset
36+
}
37+
scanIndex++
38+
}
39+
const badMatchString = str[offset + patternLastIndex]
40+
if (badMatchTable[badMatchString]) {
41+
// increase the offset if it exists
42+
offset += badMatchTable[badMatchString]
43+
} else {
44+
offset++
45+
}
46+
}
47+
return -1
48+
}
49+
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