-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
30 lines (25 loc) · 1 KB
/
Copy pathSolution.java
File metadata and controls
30 lines (25 loc) · 1 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
// Stepsort · Suffix Array
// Category: String
// Animated walkthrough: https://stepsort.prakashraj.me/algorithm/suffix-array
import java.util.Arrays;
public class Main {
// Negative when suffix i is lexicographically smaller than suffix j.
static int compareSuffixes(String text, int i, int j) {
int n = text.length();
while (i < n && j < n && text.charAt(i) == text.charAt(j)) {
i++; j++;
}
if (i >= n) return -1;
if (j >= n) return 1;
return Character.compare(text.charAt(i), text.charAt(j));
}
public static void main(String[] args) {
String text = "banana";
Integer[] sa = new Integer[text.length()];
for (int i = 0; i < sa.length; i++) sa[i] = i;
Arrays.sort(sa, (a, b) -> compareSuffixes(text, a, b));
System.out.println("text: " + text);
System.out.println("suffix array: " + Arrays.toString(sa));
for (int idx : sa) System.out.println(idx + ": " + text.substring(idx));
}
}