/
KMPAlgorithm.java
61 lines (57 loc) · 1.92 KB
/
KMPAlgorithm.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
package org.redquark.techinterview.dsa.string;
/**
* @author Anirudh Sharma
* <p>
* Given a text t[0..n-1] and a pattern p[0..m-1], write a function search(char p[], char t[])
* that prints all occurrences of p[] in t[]. You may assume that n > m.
*/
public class KMPAlgorithm {
private static boolean search(String pattern, String text) {
// Prepare the pi table of prefix-suffix table
int[] lps = findLPS(pattern);
// Indices to move through text and pattern
// respectively
int textIndex = 0;
int patternIndex = 0;
// Loop until we reach to the end
while (textIndex < text.length() && patternIndex < pattern.length()) {
// If the characters match
if (text.charAt(textIndex) == pattern.charAt(patternIndex)) {
textIndex++;
patternIndex++;
} else {
if (patternIndex != 0) {
patternIndex = lps[patternIndex - 1];
} else {
textIndex++;
}
}
}
return patternIndex == pattern.length();
}
private static int[] findLPS(String pattern) {
int[] lps = new int[pattern.length()];
// Index for checking the pattern
int index = 0;
for (int i = 1; i < pattern.length(); ) {
if (pattern.charAt(i) == pattern.charAt(index)) {
lps[i] = index + 1;
index++;
i++;
} else {
if (index != 0) {
index = lps[index - 1];
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
public static void main(String[] args) {
String text = "abcxabcdabcdabcy";
String pattern = "abcdabcy";
System.out.println("Does pattern exist? " + search(pattern, text));
}
}