Skip to content

Commit 85a2df4

Browse files
authored
To add Suffix array algorithm implementation (#6530)
* adding Suffix Array Implementation * adding test for Suffix Array * patch: lint fix in SuffixArray.java * patch: lint fix in SuffixArrayTest.java * fix: private constructor update * patch: lint fix in suffix array impl * fix: lint issue line blankspace removal * fix: checkstype issue * fix: lint format issue * Update DIRECTORY.md
1 parent c370101 commit 85a2df4

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@
744744
- 📄 [Rotation](src/main/java/com/thealgorithms/strings/Rotation.java)
745745
- 📄 [StringCompression](src/main/java/com/thealgorithms/strings/StringCompression.java)
746746
- 📄 [StringMatchFiniteAutomata](src/main/java/com/thealgorithms/strings/StringMatchFiniteAutomata.java)
747+
- 📄 [SuffixArray](src/main/java/com/thealgorithms/strings/SuffixArray.java)
747748
- 📄 [Upper](src/main/java/com/thealgorithms/strings/Upper.java)
748749
- 📄 [ValidParentheses](src/main/java/com/thealgorithms/strings/ValidParentheses.java)
749750
- 📄 [WordLadder](src/main/java/com/thealgorithms/strings/WordLadder.java)
@@ -1413,6 +1414,7 @@
14131414
- 📄 [RotationTest](src/test/java/com/thealgorithms/strings/RotationTest.java)
14141415
- 📄 [StringCompressionTest](src/test/java/com/thealgorithms/strings/StringCompressionTest.java)
14151416
- 📄 [StringMatchFiniteAutomataTest](src/test/java/com/thealgorithms/strings/StringMatchFiniteAutomataTest.java)
1417+
- 📄 [SuffixArrayTest](src/test/java/com/thealgorithms/strings/SuffixArrayTest.java)
14161418
- 📄 [UpperTest](src/test/java/com/thealgorithms/strings/UpperTest.java)
14171419
- 📄 [ValidParenthesesTest](src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java)
14181420
- 📄 [WordLadderTest](src/test/java/com/thealgorithms/strings/WordLadderTest.java)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithms.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+
private SuffixArray() {
16+
}
17+
18+
public static int[] buildSuffixArray(String text) {
19+
int n = text.length();
20+
Integer[] suffixArray = new Integer[n];
21+
int[] rank = new int[n];
22+
int[] tempRank = new int[n];
23+
24+
// Initial ranking based on characters
25+
for (int i = 0; i < n; i++) {
26+
suffixArray[i] = i;
27+
rank[i] = text.charAt(i);
28+
}
29+
30+
for (int k = 1; k < n; k *= 2) {
31+
final int step = k;
32+
33+
// Comparator: first by rank, then by rank + step
34+
Arrays.sort(suffixArray, (a, b) -> {
35+
if (rank[a] != rank[b]) {
36+
return Integer.compare(rank[a], rank[b]);
37+
}
38+
int ra = (a + step < n) ? rank[a + step] : -1;
39+
int rb = (b + step < n) ? rank[b + step] : -1;
40+
return Integer.compare(ra, rb);
41+
});
42+
43+
// Re-rank
44+
tempRank[suffixArray[0]] = 0;
45+
for (int i = 1; i < n; i++) {
46+
int prev = suffixArray[i - 1];
47+
int curr = suffixArray[i];
48+
boolean sameRank = rank[prev] == rank[curr] && ((prev + step < n ? rank[prev + step] : -1) == (curr + step < n ? rank[curr + step] : -1));
49+
tempRank[curr] = sameRank ? tempRank[prev] : tempRank[prev] + 1;
50+
}
51+
52+
System.arraycopy(tempRank, 0, rank, 0, n);
53+
54+
if (rank[suffixArray[n - 1]] == n - 1) {
55+
break;
56+
}
57+
}
58+
return Arrays.stream(suffixArray).mapToInt(Integer::intValue).toArray();
59+
}
60+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.thealgorithms.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class SuffixArrayTest {
8+
9+
@Test
10+
void testEmptyString() {
11+
int[] result = SuffixArray.buildSuffixArray("");
12+
assertArrayEquals(new int[] {}, result, "Empty string should return empty suffix array");
13+
}
14+
15+
@Test
16+
void testSingleCharacter() {
17+
int[] result = SuffixArray.buildSuffixArray("a");
18+
assertArrayEquals(new int[] {0}, result, "Single char string should return [0]");
19+
}
20+
21+
@Test
22+
void testDistinctCharacters() {
23+
int[] result = SuffixArray.buildSuffixArray("abc");
24+
assertArrayEquals(new int[] {0, 1, 2}, result, "Suffixes already in order for distinct chars");
25+
}
26+
27+
@Test
28+
void testBananaExample() {
29+
int[] result = SuffixArray.buildSuffixArray("banana");
30+
assertArrayEquals(new int[] {5, 3, 1, 0, 4, 2}, result, "Suffix array of 'banana' should be [5,3,1,0,4,2]");
31+
}
32+
33+
@Test
34+
void testStringWithDuplicates() {
35+
int[] result = SuffixArray.buildSuffixArray("aaaa");
36+
assertArrayEquals(new int[] {3, 2, 1, 0}, result, "Suffix array should be descending indices for 'aaaa'");
37+
}
38+
39+
@Test
40+
void testRandomString() {
41+
int[] result = SuffixArray.buildSuffixArray("mississippi");
42+
assertArrayEquals(new int[] {10, 7, 4, 1, 0, 9, 8, 6, 3, 5, 2}, result, "Suffix array for 'mississippi' should match expected");
43+
}
44+
}

0 commit comments

Comments
 (0)