-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP.java
More file actions
45 lines (42 loc) · 1.03 KB
/
KMP.java
File metadata and controls
45 lines (42 loc) · 1.03 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
package String;
public class KMP {
static int lps[];
public static void prefix(String a) {
lps = new int[a.length()];
int i=1, len =0;
while(i<a.length()) {
if(a.charAt(len)==a.charAt(i)) {
len++;
lps[i] = len;
i++;
}
else {
if(len==0) {
lps[i] = 0;
i++;
}
else {
len = lps[len-1];
}
}
}
}
public static boolean stringMatch(String t, String a) {
int j=0, i=0, e=-1;
while(i<t.length()) {
if(t.charAt(i) == a.charAt(j)) {
i++;
j++;
}
if(j==a.length()) {
e=i;
return true;
}
else if(i<t.length() && t.charAt(i) != a.charAt(j)) {
if(j!=0) j = lps[j-1];
else i++;
}
}
return false;
}
}