-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringMetchingAlgo.java
More file actions
45 lines (33 loc) · 1.33 KB
/
StringMetchingAlgo.java
File metadata and controls
45 lines (33 loc) · 1.33 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
44
45
public class StringMetchingAlgo {
private static int PRIME = 101 ;
private static double HashCalculation (String st) {
double hash = 0;
for (int i =0 ;i < st.length() ;i++){
hash += st.charAt(i) * Math.pow(PRIME, i) ;
}
return hash;
}
private static double updateHash (double prevHash , char oldChar , char newChar , int length) {
double newhash = (prevHash - oldChar) / PRIME ;
newhash = (newhash + newChar * Math.pow(PRIME, length -1)) ;
return newhash ;
}
private static void Search (String text , String pattern){
int len = pattern.length() ;
double patternHash = HashCalculation(pattern);
double textHash = HashCalculation(text.substring(0, len));
for (int i=0 ; i<=text.length() - len ; i++){
if (patternHash == textHash){
if (text.substring(i , i +len).equals(pattern)){
System.out.println("pattern found at :" + i);
}
}
if (i< text.length() - len) {
textHash = updateHash(textHash, text.charAt(i), text.charAt( i + len), len) ;
}
}
}
public static void main(String[] args) {
Search("ApoorvkunalRahul" , "kunal");
}
}