-
Notifications
You must be signed in to change notification settings - Fork 0
/
RabinKarpAlgorithmExample.java
123 lines (109 loc) · 2.77 KB
/
RabinKarpAlgorithmExample.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class RabinKarpAlgorithmExample
{
static final int PRIME = 101;
public static ArrayList<Integer> rabinKarp(String needle, String document)
{
ArrayList<Integer> matchedIndexes = new ArrayList<Integer>();
int key = hash(needle);
String rollingComparison;
for(int i = 0; i < document.length(); i++)
{
if(i + needle.length() < document.length())
{
rollingComparison = document.substring(i, needle.length() + i);
}
else return matchedIndexes;
if(key == hash(rollingComparison))
{
matchedIndexes.add(i);
}
}
return matchedIndexes;
}
/**
* O(m*n)
*
* Where m is the length of the pattern (needle) and n is the length of the document
*
* @param needle
* @param document
* @param returnFirstMatch
* @return
* @throws Exception
*/
public static int rabinKarp(String needle, String document, boolean returnFirstMatch) throws Exception
{
if(!returnFirstMatch)
{
throw new Exception("Must be true, otherwise do not include third parameter");
}
int matchedIndexes = -1;
int key = hash(needle);
String rollingComparison;
for(int i = 0; i < document.length(); i++)
{
if(i + needle.length() < document.length())
{
rollingComparison = document.substring(i, needle.length() + i);
}
else return matchedIndexes;
if(key == hash(rollingComparison))
{
matchedIndexes = i;
return matchedIndexes;
}
}
return matchedIndexes;
}
public static void main(String[] args)
{
try
{
String filePath = new File("").getAbsolutePath();
String doc1path = new String(filePath+"/src/miscellaneous/Rabin_Karp_Input/doc1.txt");
List<String> doc = new ArrayList<String>(Files.readAllLines(Paths.get(doc1path)));
String document = appendDocument(doc);
int results = rabinKarp("test", document, true);
System.out.println(results);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
// ============================ UTILITIES ======================== //
/**
* Returns one long string of a given document
*
* @param document
* @return
*/
public static String appendDocument(List<String> document)
{
String doc = "";
for(String line : document)
{
doc = doc.concat(line+" ");
}
return doc;
}
public static int hash(String key)
{
int value = 0;
for (int i = 0; i < key.length(); i++)
{
char c = key.charAt(i);
value += (int) c * Math.pow(getPrime(), i);
}
return value;
}
public static int getPrime()
{
return RabinKarpAlgorithmExample.PRIME;
}
}