Java Program to Implement Suffix Array Last Updated : 27 May, 2024 Comments Improve Suggest changes Like Article Like Report A Suffix Array is a fundamental data structure used in string processing and computational biology. It represents an array of all suffixes of a given string, sorted lexicographically. Suffix arrays find applications in pattern matching, substring search, text compression, and bioinformatics, among others. Organization of Suffix ArrayA suffix array is essentially an array of integers, each representing the starting index of a suffix in the original string. The array is sorted in lexicographical order, so it provides an efficient way to search for substrings within the string. Here's an illustration: Let's take an example string "banana": The corresponding suffix array for "banana" would be: [ 5 , 3 , 1 , 0 , 4 , 2 ] Implementation of Suffix ArrayThe implementation of a suffix array can be done efficiently using various algorithms such as the Manber-Myers algorithm, the Skew algorithm, or the SA-IS algorithm. Here's a simplified explanation of how it can be implemented: Construct Suffixes : Generate all suffixes of the given string.Sort Suffixes : Sort the generated suffixes lexicographically.Construct Suffix Array : Create an array that stores the indices of the sorted suffixes.Basic Operations and ComplexityOperation Description Complexity Search Suffix arrays enable efficient substring search using techniques like binary search. Time complexity: O(m * log n), where m is the length of the query string and n is the length of the original string. Space Complexity: O(n), where n is the length of the original string. Program to Implement Suffix ArrayBelow is the Program to Implement Suffix Array: Java // Java Program to Implement Suffix Array import java.util.*; // Driver Class class SuffixArray { public static int[] constructSuffixArray(String text) { // Get the length of the input text int n = text.length(); // Create an array of Integer objects to store indices Integer[] helper = new Integer[n]; // Initialize the array with indices from 0 to n-1 for (int i = 0; i < n; i++) helper[i] = i; // Sort the array of indices based on the suffixes Arrays.sort(sa, (a, b) -> text.substring(a) .compareTo(text.substring(b))); // Convert the Integer array to an int array int[] suffixArray = new int[n]; for (int i = 0; i < n; i++) suffixArray[i] = helper[i]; return suffixArray; } public static void main(String[] args) { // Example input text String text = "banana$"; // Construct the suffix array int[] suffixArray = constructSuffixArray(text); // Print the suffix array System.out.println("Suffix Array for \"" + text + "\": " + Arrays.toString(suffixArray)); } } OutputApplications of Suffix Array Pattern Matching: Efficiently find occurrences of substrings within a text.Substring Search: Quickly locate substrings within large texts.Bioinformatics: Analyze DNA sequences for patterns and mutations.Text Compression: Used in algorithms like Burrows-Wheeler Transform. Comment More infoAdvertise with us Next Article Java Program to Implement Suffix Array G gorantashreya Follow Improve Article Tags : Java Java-DSA Practice Tags : Java Similar Reads C++ Program to Implement Suffix Array A suffix array is a data structure which can be used to store all possible suffixes of a given string in the sorted order. It stores the starting indices of the suffixes in lexicographical order. It is similar to trie data structure but is more space efficient then tries.ExampleLet the given string 3 min read How to Get Last Element in Array in Java? In Java, to get the last element in an array, we can access the element at the index array.length - 1 using array indexing. The length property of the array provides its total size, and subtracting one from it gives the index of the last element.Example 1: Here, we will access the last element in an 2 min read Java Multi-Dimensional Arrays Multidimensional arrays are used to store the data in rows and columns, where each row can represent another individual array are multidimensional array. It is also known as array of arrays. The multidimensional array has more than one dimension, where each row is stored in the heap independently. T 10 min read How to sort an Array of Strings in Java Array Of StringsTo sort an array of strings in Java, we can use Arrays.sort() function. Java // A sample Java program to // sort an array of strings // in ascending and descending // orders using Arrays.sort(). import java.util.Arrays; import java.util.Collections; // Driver Class public class SortE 3 min read Java program to expand a String if range is given? Suppose we have given a string in which some ranges as specified and we have to place the numbers which is between the given range in the specified place as provided and depicted in the illustration below as follows for a better understanding. Illustration: Input : string x = "1-5, 8, 11-14, 18, 20, 6 min read Output of Java Programs | Set 47 (Arrays) Prerequisite : Arrays in Java Question 1. What is the output of this question? JAVA class Test1 { public static void main(String[] args) { int arr[] = { 11, 22, 33 }; for (int i = 0; i < arr.length; i++) System.out.print(arr[i] + " "); System.out.println(); int arr2[] = new int[3]; arr2 2 min read Arraylist lastIndexOf() Method in Java with Examples In Java, the lastIndexOf() method is used to find the index of the last occurrence of a specified element in an ArrayList.Example 1: Here, we will use the lastIndexOf() method to find the index of the last occurrence of a specific element in an ArrayList of integers.Java// Java program to demonstrat 3 min read String Arrays in Java A String Array in Java is an array that stores string values. The string is nothing but an object representing a sequence of char values. Strings are immutable in Java, this means their values cannot be modified once created.When we create an array of type String in Java, it is called a String Array 5 min read ArrayDeque offerFirst() Method in Java The Java.util.ArrayDeque.offerFirst(Object element) method in Java is used to add a specific element at the front of the Deque. The function is similar to the addFirst() method of ArrayDeque in Java. Syntax: Array_Deque.offerFirst(Object element) Parameters: The parameter element is of the type Arra 2 min read Java Arrays Coding Practice Problems Arrays are a fundamental data structure in Java programming, enabling efficient storage, manipulation, and retrieval of elements. This collection of Java array practice problems covers essential operations, including array traversal, sorting, searching, matrix manipulations, and element-wise calcula 2 min read Like