Repository navigation
Expand file tree
/
Copy pathSolution.java
More file actions
63 lines (59 loc) · 1.91 KB
/
Copy pathSolution.java
File metadata and controls
63 lines (59 loc) · 1.91 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
// Stepsort · KMP Pattern Matching
// Category: String
// Animated walkthrough: https://stepsort.prakashraj.me/algorithm/kmp
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
// lps[i] = length of the longest proper prefix of pattern[0..i]
// that is also a suffix of it.
static int[] buildLps(String pattern) {
int m = pattern.length();
int[] lps = new int[m];
int length = 0;
int i = 1;
while (i < m) {
if (pattern.charAt(i) == pattern.charAt(length)) {
length++;
lps[i] = length;
i++;
} else if (length > 0) {
length = lps[length - 1];
} else {
i++;
}
}
return lps;
}
// Scan the text once, falling back along the LPS table on mismatch.
static List<Integer> kmpSearch(String text, String pattern) {
List<Integer> matches = new ArrayList<>();
int n = text.length(), m = pattern.length();
if (m == 0 || m > n) return matches;
int[] lps = buildLps(pattern);
int i = 0, j = 0;
while (i < n) {
if (text.charAt(i) == pattern.charAt(j)) {
i++;
j++;
if (j == m) {
matches.add(i - m);
j = lps[j - 1];
}
} else if (j > 0) {
j = lps[j - 1];
} else {
i++;
}
}
return matches;
}
public static void main(String[] args) {
String text = "AABAACAADAABAABA";
String pattern = "AABA";
System.out.println("text: " + text);
System.out.println("pattern: " + pattern);
System.out.println("lps table: " + Arrays.toString(buildLps(pattern)));
System.out.println("found at: " + kmpSearch(text, pattern));
}
}