Find the Suffix Array of given String with no repeating character


In this problem, we will find the suffix array of the given string. We can sort all the string's suffixes using their first character, as the input string contains different characters.

Problem statement - We have given a string containing the different alphabetical characters, and we need to find the suffix array of the given string. The suffix array of the string is a sorted array containing all suffixes of the given string.

Sample examples

Input

str = "point";

Output

2 3 1 0 4

Explanation

  • All suffixes of the string are 'point', 'oint', 'int', 'nt', and 't'.

  • The sorted order of the suffixes is 'int', 'nt', 'oint', 'point', and 't'.

  • We printed the starting index of suffixes, which are 2 3 1 0 4, according to the sorted order of suffixes.

Input

str = "dbce";

Output

1 2 0 3

Explanation

  • All suffixes are 'e', 'ce', 'bce', and 'dbce'.

  • The sorted order of suffixes is 'bce', 'ce', 'dbce', and 'e'.

  • The index of the suffixes according to the sorted order is 1, 2, 0, 2.

Approach 1

In this approach, we will keep track of the character presence of the string using the array. After that, we will calculate the cumulative frequency, and based on that, we will get the starting indices of suffixes in the sorted order.

Algorithm

Step 1 - Define the 'freq' array of length 26 and initialize all elements with zero.

Step 2 - Count the frequency of each character and store it in the freq array.

Step 3 - Now, traverse the freq array and add the previous element's value to the current element.

Step 4 - Define the 'move' array and initialize the first element with zero. In the move array, we need to store each element of the freq array by moving each element to the next index. It means we need to perform move[p+1] = freq[p] for all elements.

Step 5 - Now, define the 'indexs' array to store the indexes of the sorted suffix.

Step 6 - Traverse the string in reverse order to take each suffix. In the loop, take the rank of the current character from the 'move' array and store the 'p' at that particular index.

Step 7 - Print all values of the 'indexs' array.

Example

#include <bits/stdc++.h>
using namespace std;

void printSuffixArray(string alpha, int len) {
   // Calculate the frequency of each character of the string
   int freq[26] = {0};
   for (int p = 0; p < len; p++) {
      freq[alpha[p] - 'a']++;
   }
   // Finding the cumulative frequency of each character
   for (int p = 1; p < 26; p++) {
      freq[p] = freq[p] + freq[p - 1];
   }
   int move[26];
   move[0] = 0;
   for (int p = 0; p < 25; p++) {
      move[p + 1] = freq[p];
   }
   int indexs[len] = {0};
   // Traverse the string in reverse order
   for (int p = len - 1; p >= 0; p--) {
      // Storing suffix array in indexs[]
      indexs[move[alpha[p] - 'a']] = p;
   }
   // Print sorted suffix
   for (int p = 0; p < len; p++)
      cout << indexs[p] << " ";
}
int main(){
   string str = "point";
   int len = str.length();
   printSuffixArray(str, len);
   return 0;
}

Output

2 3 1 0 4

Time complexity - O(N) as we traverse the string.

Space complexity - O(N) as we use arrays.

Updated on: 24-Aug-2023

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements