-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Rabin Karp Search Algorithm #1545
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
7 commits
Select commit
Hold shift + click to select a range
67cbd8e
Search: Rabin-Karp algorithm
aladin002dz c223425
Prettier Style
aladin002dz 30821e0
Search: Rabin-Karp adding reference
aladin002dz a31f9ec
Search: Rabin-Karp styling and remove unecessary logging
aladin002dz 5d2706a
Search: Rabin-Karp review notes
aladin002dz 00726c3
Simplify return
appgurueu 952fbd4
Updated Documentation in README.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ function BinaryCountSetBits(a) { | |
|
||
let count = 0 | ||
while (a) { | ||
a &= (a - 1) | ||
a &= a - 1 | ||
count++ | ||
} | ||
|
||
|
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 |
---|---|---|
|
@@ -35,6 +35,6 @@ export const isAutomorphic = (n) => { | |
n = Math.floor(n / 10) | ||
n_sq = Math.floor(n_sq / 10) | ||
} | ||
|
||
return true | ||
} |
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 |
---|---|---|
|
@@ -36,4 +36,4 @@ export function interpolationSearch(arr, key) { | |
} | ||
|
||
return -1 | ||
} | ||
} |
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,64 @@ | ||
/* | ||
* Implements the Rabin-Karp algorithm for pattern searching. | ||
* | ||
* The Rabin-Karp algorithm is a string searching algorithm that uses hashing to find patterns in strings. | ||
* It is faster than naive string matching algorithms because it avoids comparing every character in the text. | ||
* | ||
* This implementation uses a rolling hash function to efficiently compute the hash values of substrings. | ||
* It also uses a modulo operator to reduce the size of the hash values, which helps to prevent hash collisions. | ||
* | ||
* The algorithm returns an array of indices where the pattern is found in the text. If the pattern is not | ||
* found, the algorithm returns an empty array. | ||
* | ||
* [Reference](https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm) | ||
*/ | ||
|
||
const BASE = 256 // The number of characters in the alphabet | ||
const MOD = 997 // A prime number used for the hash function | ||
|
||
function rabinKarpSearch(text, pattern) { | ||
const patternLength = pattern.length | ||
const textLength = text.length | ||
const hashPattern = hash(pattern, patternLength) | ||
const hashText = [] | ||
const indices = [] | ||
|
||
// Calculate the hash of the first substring in the text | ||
hashText[0] = hash(text, patternLength) | ||
|
||
// Precompute BASE^(patternLength-1) % MOD | ||
const basePow = Math.pow(BASE, patternLength - 1) % MOD | ||
|
||
for (let i = 1; i <= textLength - patternLength + 1; i++) { | ||
// Update the rolling hash by removing the first character | ||
// and adding the next character in the text | ||
hashText[i] = | ||
(BASE * (hashText[i - 1] - text.charCodeAt(i - 1) * basePow) + | ||
text.charCodeAt(i + patternLength - 1)) % | ||
MOD | ||
|
||
// In case of hash collision, check character by character | ||
if (hashText[i] < 0) { | ||
hashText[i] += MOD | ||
} | ||
|
||
// Check if the hashes match and perform a character-wise comparison | ||
if (hashText[i] === hashPattern) { | ||
if (text.substring(i, i + patternLength) === pattern) { | ||
indices.push(i) // Store the index where the pattern is found | ||
} | ||
} | ||
} | ||
|
||
return indices | ||
} | ||
|
||
function hash(str, length) { | ||
let hashValue = 0 | ||
for (let i = 0; i < length; i++) { | ||
hashValue = (hashValue * BASE + str.charCodeAt(i)) % MOD | ||
} | ||
return hashValue | ||
} | ||
|
||
export { rabinKarpSearch } |
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,30 @@ | ||
import { rabinKarpSearch } from '../RabinKarp' | ||
|
||
describe('Rabin-Karp Search', function () { | ||
it('should find the pattern in the text', function () { | ||
const text = 'ABABDABACDABABCABAB' | ||
const pattern = 'DAB' | ||
const expected = [4, 9] | ||
|
||
const result = rabinKarpSearch(text, pattern) | ||
expect(result).to.deep.equal(expected) | ||
}) | ||
|
||
it('should handle multiple occurrences of the pattern', function () { | ||
const text = 'ABABABABABAB' | ||
const pattern = 'ABAB' | ||
const expected = [2, 4, 6, 8] | ||
|
||
const result = rabinKarpSearch(text, pattern) | ||
expect(result).to.deep.equal(expected) | ||
}) | ||
|
||
it('should handle pattern not found', function () { | ||
const text = 'ABCD' | ||
const pattern = 'XYZ' | ||
const expected = [] | ||
|
||
const result = rabinKarpSearch(text, pattern) | ||
expect(result).to.deep.equal(expected) | ||
}) | ||
}) |
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.