-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrabin-karp.js
More file actions
43 lines (39 loc) · 1.32 KB
/
rabin-karp.js
File metadata and controls
43 lines (39 loc) · 1.32 KB
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
40
41
42
43
let fs = require('fs');
let arg = process.argv;
let str = fs.readFileSync(arg[2]).toString();
let key = fs.readFileSync(arg[3]).toString();
let collisions = 0;
let counter = 0;
const hash3 = () => {
let arr = new Array();
let codeStr = 0, codeKey = 0;
let len_key = key.length, len_str = str.length;
for (let i = 0; i < len_key; i++) {
codeKey += key.charCodeAt(i) * Math.pow(2, len_key - i - 1);
codeStr += str.charCodeAt(i) * Math.pow(2, len_key - i - 1);
}
for (i = 1; i <= len_str - len_key + 1; i++) {
if (codeStr == codeKey) {
for (j = 0; j < len_key; j++) {
if (str.charAt(j + i - 1) == key.charAt(j)) {
if (j == len_key - 1) {
arr.push(i - 1);
counter++;
}
}
else {
collisions++;
break;
}
}
}
codeStr = (codeStr - str.charCodeAt(i - 1) * Math.pow(2, len_key - 1)) * 2 + str.charCodeAt(len_key + i - 1);
}
return arr.slice(0, 10);
}
console.log("Hash3:");
console.time('Time');
console.log(hash3().join(', '));
console.timeEnd('Time');
console.log("collisions:", collisions);
console.log("counter:", counter);