Lexicographic rank of a string among all its substrings


String manipulation is an essential topic in computer science that involves operations such as concatenation, substring, reversing, and more. One interesting problem related to string manipulation is to find the lexicographic rank of a string among all its substrings. In this article, we will discuss an algorithm to solve this problem using recursion and backtracking.

Problem Statement

Given a string S of length N, we have to find the lexicographic rank of S among all its substrings. The lexicographic rank is defined as the position of a string in the lexicographically sorted list of all its substrings.

Approach

We can solve this problem using a recursive and backtracking approach. We will generate all possible substrings of the given string S and keep track of the number of substrings that come before S in lexicographic order.

Let's see the algorithm in detail −

  • Initialize a variable "rank" to 1.

  • Generate all possible substrings of the given string S using recursion and backtracking.

  • For each substring, compare it with the given string S and increment the "rank" variable if the substring comes before S in lexicographic order.

  • Return the "rank" variable.

Example

Here's the C++ code to implement the above algorithm −

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

void generateSubstrings(string s, string temp, int start, int end, int& rank) {
   if (start > end) {
      if (temp < s) {
         rank++;
      }
      return;
   }
   
   generateSubstrings(s, temp, start + 1, end, rank);
   generateSubstrings(s, temp + s[start], start + 1, end, rank);
}

int lexicographicRank(string s) {
   int rank = 1;
   generateSubstrings(s, "", 0, s.length() - 1, rank);
   return rank;
}

int main() {
   string s = "abc";
   cout << "String: " << s << endl;
   cout << "Lexicographic Rank: " << lexicographicRank(s) << endl;
   return 0;
}

Output

String: abc
Lexicographic Rank: 4

Explanation of Test Case

Let's take the string "abc" as an example. We need to find the lexicographic rank of all its substrings.

Initially, we have an empty string, which has a lexicographic rank of 1.

The substring "a" has a lexicographic rank of 1, as it is the first substring in lexicographic order.

The substring "ab" has a lexicographic rank of 2, as it comes after "a" in lexicographic order.

The substring "abc" has a lexicographic rank of 4, as it comes after "a", "ab", and "ac" in lexicographic order.

The substring "b" has a lexicographic rank of 3, as it comes after "a" but before "c" in lexicographic order.

The substring "bc" has a lexicographic rank of 5, as it comes after "ab" and "ac" but before "c" in lexicographic order.

The substring "c" has a lexicographic rank of 6, as it comes after "a", "ab", "ac", and "b" in lexicographic order.

Therefore, the lexicographic rank of all substrings of "abc" is: [1, 1, 2, 3, 4, 5, 6].

Conclusion

In this article, we have discussed an algorithm to find the lexicographic rank of a string among all its substrings using recursion and backtracking. This approach can be used to solve other problems related to string manipulation as well.

Updated on: 18-May-2023

200 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements