-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathKMP.java
More file actions
60 lines (53 loc) · 1.61 KB
/
KMP.java
File metadata and controls
60 lines (53 loc) · 1.61 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class KMP {
// Function to compute LPS array
public static int[] computeLPSArray(String pattern) {
int M = pattern.length();
int[] lps = new int[M];
int len = 0; // length of the previous longest prefix suffix
int i = 1;
lps[0] = 0; // lps[0] is always 0
while (i < M) {
if (pattern.charAt(i) == pattern.charAt(len)) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
// KMP Search function
public static void KMPSearch(String pattern, String text) {
int M = pattern.length();
int N = text.length();
int[] lps = computeLPSArray(pattern);
int i = 0; // index for text
int j = 0; // index for pattern
while (i < N) {
if (pattern.charAt(j) == text.charAt(i)) {
i++;
j++;
}
if (j == M) {
System.out.println("Pattern found at index " + (i - j));
j = lps[j - 1];
} else if (i < N && pattern.charAt(j) != text.charAt(i)) {
if (j != 0)
j = lps[j - 1];
else
i++;
}
}
}
public static void main(String[] args) {
String text = "ABABDABACDABABCABAB";
String pattern = "ABABCABAB";
KMPSearch(pattern, text);
}
}