-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKMP.java
79 lines (68 loc) · 2.31 KB
/
KMP.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Problem : https://www.geeksforgeeks.org/kmp-algorithm-for-pattern-searching/
* https://www.techiedelight.com/implementation-kmp-algorithm-c-cpp-java/
*
* For computation and explaination watch this video
* https://www.youtube.com/watch?v=BXCEFAzhxGY&feature=em-comments
*/
import java.util.List;
import java.util.LinkedList;
class Search {
// To work through this method watch video and try to create array on
// example DADAXDADADZ
private static int[] computeLPS(String s) {
if (s == null) {
throw new IllegalArgumentException();
}
int max = 0;
int n = s.length();
int[] lps = new int[n];
int index = 1;
while (index < n) {
if (s.charAt(max) == s.charAt(index)) {
lps[index] = ++max;
index++;
} else if (max != 0) {
max = lps[max - 1];
} else {
lps[index] = 0;
index++;
}
}
return lps;
}
private static void printArray(int[] array) {
for (int i : array) {
System.out.print(i + " ");
}
System.out.println("");
}
public static List<Integer> searchPattern(String text, String pattern) {
if (text == null || pattern == null) {
throw new IllegalArgumentException();
}
int[] lps = computeLPS(pattern);
List<Integer> list = new LinkedList<>();
for (int i = 0, j = 0; i < text.length(); ++i) {
if (j < pattern.length() && text.charAt(i) == pattern.charAt(j)) {
if (++j == pattern.length()) {
list.add(i - j + 1);
}
} else if (j > 0) {
j = lps[j - 1];
i--;
}
}
return list;
}
}
public class KMP {
public static void main(String[] args) {
String[] text = {"DADAXDADADZ", "ABCABAABCABAC", "ABABDABACDABABCABAB"};
String[] pattern = {"DA", "CAB", "ABABCABAB"};
for (int i = 0; i < text.length; ++i) {
List<Integer> list = Search.searchPattern(text[i], pattern[i]);
System.out.println(pattern[i] + " appeared in " + text[i] + " with at an index " + list);
}
}
}