You are given a string, find its rank among all its permutations sorted lexicographically.
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.
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Lexicographic rank of a string
- Lexicographic rank of a string with duplicate characters
- Find a string in lexicographic order which is in between given two strings
- Print a number as string of 'A' and 'B' in lexicographic order
- Generating distinct subsequences of a given string in lexicographic order
- Find the N-th lexicographic permutation of string using Factoradic method
- Power Set in Lexicographic order
- Print all permutations in sorted (lexicographic) order
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- String slicing in Python to check if a string can become empty by recursive deletion
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Create a new string by alternately combining the characters of two halves of the string in reverse
- String Range Queries to find the number of subsets equal to a given String
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Transform string str1 into str2 by taking characters from string str3
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Minimal moves to form a string by adding characters or appending string itself
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Find the count of palindromic sub-string of a string in its sorted form
Improved By : Akanksha_Rai