-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMPAlgorithm.java
More file actions
67 lines (57 loc) · 1.82 KB
/
KMPAlgorithm.java
File metadata and controls
67 lines (57 loc) · 1.82 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
61
62
63
64
65
66
67
public class KMPAlgorithm {
private int[] computePrefixArray(String pattern) {
int m = pattern.length();
int[] prefixArray = new int[m];
int j = 0;
for (int i = 1; i < m; i++) {
if (pattern.charAt(i) == pattern.charAt(j)) {
j++;
prefixArray[i] = j;
} else {
if (j > 0) {
j = prefixArray[j - 1];
i--; // Recheck the current character
} else {
prefixArray[i] = 0;
}
}
}
return prefixArray;
}
public int searchSubstring(String text, String pattern) {
int n = text.length();
int m = pattern.length();
int[] prefixArray = computePrefixArray(pattern);
int i = 0; // Index for text
int j = 0; // Index for pattern
while (i < n) {
if (text.charAt(i) == pattern.charAt(j)) {
i++;
j++;
}
if (j == m) {
// Pattern found, return the starting index
return i - j;
} else if (i < n && text.charAt(i) != pattern.charAt(j)) {
if (j > 0) {
j = prefixArray[j - 1];
} else {
i++;
}
}
}
// Pattern not found
return -1;
}
public static void main(String[] args) {
String text = "ABCABCDABABCDABCDABDE";
String pattern = "ABCDABD";
KMPAlgorithm kmp = new KMPAlgorithm();
int index = kmp.searchSubstring(text, pattern);
if (index != -1) {
System.out.println("Pattern found at index: " + index);
} else {
System.out.println("Pattern not found in the text.");
}
}
}