Lexicographic rank of a string using STL
You are given a string, find its rank among all its permutations sorted lexicographically. Examples:
Input : str = "acb" Output : Rank = 2 Input : str = "string" Output : Rank = 598 Input : str = "cba" Output : Rank = 6
We have already discussed solutions to find Lexicographic rank of string In this post, we use the STL function “next_permutation ()” to generate all possible permutations of the given string and, as it gives us permutations in lexicographic order, we will put an iterator to find the rank of each string. While iterating when Our permuted string becomes identical to the original input string, we break from the loop and the iterator value for the last iteration is our required result.
Time Complexity: O(n log n)
Auxiliary Space: O(1)
This article is contributed by Aarti_Rathi and Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.