-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabin-karp.js
39 lines (33 loc) · 1.07 KB
/
rabin-karp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const prime = 101; // A prime number for hashing
function rabinKarp(text, pattern) {
const n = text.length;
const m = pattern.length;
const patternHash = hash(pattern, m);
const textHashes = [];
// Compute initial hash for all substrings of length m in text
for (let i = 0; i <= n - m; i++) {
textHashes.push(hash(text.substring(i, i + m), m));
}
// Compare hash values and check for exact match
for (let i = 0; i <= n - m; i++) {
if (patternHash === textHashes[i] && pattern === text.substring(i, i + m)) {
return i; // Pattern found at index i
}
}
return -1; // Pattern not found
}
function hash(str, len) {
let hashValue = 0;
for (let i = 0; i < len; i++) {
hashValue += str.charCodeAt(i) * Math.pow(prime, i);
}
return hashValue;
}
const text = "AABAACAADAABAABA";
const pattern = "AABA";
const index = rabinKarp(text, pattern);
if (index !== -1) {
console.log("Pattern", pattern, "found at index:", index);
} else {
console.log("Pattern", pattern, "not found in the text.");
}