-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
51 lines (45 loc) · 1.7 KB
/
Copy pathSolution.java
File metadata and controls
51 lines (45 loc) · 1.7 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
// Stepsort · Z Algorithm
// Category: String
// Animated walkthrough: https://stepsort.prakashraj.me/algorithm/z-algorithm
import java.util.Arrays;
public class Main {
// z[i] = length of the longest common prefix of s and s[i..].
static int[] zFunction(String s) {
int n = s.length();
int[] z = new int[n];
z[0] = n;
int left = 0, right = 0; // rightmost match window found so far
for (int i = 1; i < n; i++) {
if (i < right) { // reuse information from the previous window
z[i] = Math.min(right - i, z[i - left]);
}
while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i])) {
z[i]++;
}
if (i + z[i] > right) { // extend the match window
left = i;
right = i + z[i];
}
}
return z;
}
public static void main(String[] args) {
String text = "aabxaabxcaabxaabxay";
String pattern = "aabx";
// A '#' separator never appears in the data, so any maximal
// prefix match crossing it must end exactly at the separator.
String combined = pattern + "#" + text;
int[] z = zFunction(combined);
int m = pattern.length();
StringBuilder matches = new StringBuilder();
for (int i = m + 1; i < combined.length(); i++) {
if (z[i] == m) {
if (matches.length() > 0) matches.append(" ");
matches.append(i - m - 1);
}
}
System.out.println("combined: " + combined);
System.out.println("z-array: " + Arrays.toString(z));
System.out.println("matches: " + matches.toString());
}
}