-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Add Boyer-Moore string search algorithm #990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
277464f
Auto-update DIRECTORY.md
4f16a9b
Merge branch 'TheAlgorithms:master' into master
ayDavidGitHere 2e63444
Auto-update DIRECTORY.md
e7cb578
Created boyerMoore.js
ayDavidGitHere 8111a43
Update and rename boyerMoore.js to BoyerMoore.js
ayDavidGitHere 81e72f6
Update BooyerMoore.js
ayDavidGitHere 30f7486
Update BoyerMoore.js
ayDavidGitHere 95a902a
Final fix codestyle for BoyerMoore.js
ayDavidGitHere 508f38d
Auto-update DIRECTORY.md
40bd41c
Update BoyerMoore.js | Lgtm alert fix.
ayDavidGitHere 9181bde
BoyerMoore.js| Final fix indentation.
ayDavidGitHere 1992b41
BoyerMoore.js| Final Fix :(
ayDavidGitHere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* | ||
* | ||
*Implementation of the Boyer-Moore String Search Algorithm. | ||
*The Boyer–Moore string search algorithm allows linear time in | ||
*search by skipping indices when searching inside a string for a pattern. | ||
* | ||
* | ||
* | ||
* | ||
**/ | ||
const buildBadMatchTable = (str) => { | ||
const tableObj = {} | ||
const strLength = str.length | ||
for (let i = 0; i < strLength - 1; i++) { | ||
tableObj[str[i]] = strLength - 1 - i | ||
} | ||
if (tableObj[str[strLength - 1]] === undefined) { | ||
tableObj[str[strLength - 1]] = strLength | ||
} | ||
return tableObj | ||
} | ||
|
||
const boyerMoore = (str, pattern) => { | ||
const badMatchTable = buildBadMatchTable(pattern) | ||
let offset = 0 | ||
const patternLastIndex = pattern.length - 1 | ||
const maxOffset = str.length - pattern.length | ||
// if the offset is bigger than maxOffset, cannot be found | ||
while (offset <= maxOffset) { | ||
let scanIndex = 0 | ||
while (pattern[scanIndex] === str[scanIndex + offset]) { | ||
if (scanIndex === patternLastIndex) { | ||
// found at this index | ||
return offset | ||
} | ||
scanIndex++ | ||
} | ||
const badMatchString = str[offset + patternLastIndex] | ||
if (badMatchTable[badMatchString]) { | ||
// increase the offset if it exists | ||
offset += badMatchTable[badMatchString] | ||
} else { | ||
offset++ | ||
} | ||
} | ||
return -1 | ||
} | ||
export { boyerMoore } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.