Skip to content

Commit 76f7a42

Browse files
authored
adding Suffix Array Implementation
1 parent c370101 commit 76f7a42

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithm.strings;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Suffix Array implementation in Java.
7+
* Builds an array of indices that represent all suffixes of a string in sorted order.
8+
* Wikipedia Reference: https://en.wikipedia.org/wiki/Suffix_array
9+
* Author: Nithin U.
10+
* Github: https://github.com/NithinU2802
11+
*/
12+
13+
public final class SuffixArray {
14+
15+
public static int[] buildSuffixArray(String text) {
16+
int n = text.length();
17+
Integer[] suffixArray = new Integer[n];
18+
int[] rank = new int[n];
19+
int[] tempRank = new int[n];
20+
21+
// Initial ranking based on characters
22+
for (int i = 0; i < n; i++) {
23+
suffixArray[i] = i;
24+
rank[i] = text.charAt(i);
25+
}
26+
27+
for (int k = 1; k < n; k *= 2) {
28+
final int step = k;
29+
30+
// Comparator: first by rank, then by rank + step
31+
Arrays.sort(suffixArray, (a, b) -> {
32+
if (rank[a] != rank[b])
33+
return Integer.compare(rank[a], rank[b]);
34+
int ra = (a + step < n) ? rank[a + step] : -1;
35+
int rb = (b + step < n) ? rank[b + step] : -1;
36+
return Integer.compare(ra, rb);
37+
});
38+
39+
// Re-rank
40+
tempRank[suffixArray[0]] = 0;
41+
for (int i = 1; i < n; i++) {
42+
int prev = suffixArray[i - 1];
43+
int curr = suffixArray[i];
44+
boolean sameRank = rank[prev] == rank[curr] &&
45+
((prev + step < n ? rank[prev + step] : -1) == (curr + step < n ? rank[curr + step] : -1));
46+
tempRank[curr] = sameRank ? tempRank[prev] : tempRank[prev] + 1;
47+
}
48+
49+
System.arraycopy(tempRank, 0, rank, 0, n);
50+
51+
if (rank[suffixArray[n - 1]] == n - 1)
52+
break;
53+
}
54+
return Arrays.stream(suffixArray).mapToInt(Integer::intValue).toArray();
55+
}
56+
57+
}

0 commit comments

Comments
 (0)