![]() |
VOOZH | about |
Given string str, the task is to find the rank of the given string among all its substrings arranged lexicographically.
Examples:
Input: S = "enren"
Output: 7
Explanation:
All the possible substrings in the sorted order are {"e", "e", "en", "en", "enr", "enre", "enren", "n", "n", "nr", "nre", "nren", "r", "re", "ren"}.
Therefore, the rank of the given string "enren" is 7.Input: S = "geeks"
Output: 12
Explanation:
All possible substrings in the sorted order are {"e", "e", "ee", "eek", "eeks", "ek", "eks", "g", "ge", "gee", "geek", "geeks", "k", "ks", "s"}.
Therefore, the rank of the given string "geeks" is 12.
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
7
Time Complexity: O(N3)
Auxiliary Space: O(N)