Suffix array: find the needle in the hay
Suffix array
By definition, a suffix array (denoted as sa) contains the starting indices of all suffixes. It’s simply an int array where sa[i] represents the starting index of the corresponding suffix.
Taking fizzbuzz as an example, its suffix array is 4 0 1 5 7 3 6 2. The details are shown in the following table.
Suffix array sa |
Corresponding suffix |
|---|---|
| 4 | buzz |
| 0 | fizzbuzz |
| 1 | izzbuzz |
| 5 | uzz |
| 7 | z |
| 3 | zbuzz |
| 6 | zz |
| 2 | zzbuzz |
How to generate a suffix array
Naive way
Let $N$ represent the length of the string. The naive algorithm is straightforward: generate all possible suffixes and then sort them using an efficient sorting algorithm. This requires one sort iteration, which may involve $O(N log\ N)$ comparisons. In the worst case, each string comparison takes $O(N)$ time. Therefore, the overall time complexity would be