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.

`// C++ program to print rank of ` `// string using next_permute() ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print rank of string ` `// using next_permute() ` `int` `findRank(string str) ` `{ ` ` ` `// store original string ` ` ` `string orgStr = str; ` ` ` ` ` `// Sort the string in lexicographically ` ` ` `// ascending order ` ` ` `sort(str.begin(), str.end()); ` ` ` ` ` `// Keep iterating until ` ` ` `// we reach equality condition ` ` ` `long` `int` `i = 1; ` ` ` `do` `{ ` ` ` `// check for nth iteration ` ` ` `if` `(str == orgStr) ` ` ` `break` `; ` ` ` ` ` `i++; ` ` ` `} ` `while` `(next_permutation(str.begin(), str.end())); ` ` ` ` ` `// return iterator value ` ` ` `return` `i; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"GEEKS"` `; ` ` ` `cout << findRank(str); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

25

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 contribute@geeksforgeeks.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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Lexicographic rank of a string
- Lexicographic rank of a string among all its substrings
- Lexicographic rank of a string with duplicate characters
- Lexicographic smallest permutation of a String containing the second String as a Substring
- Find the N-th lexicographic permutation of string using Factoradic method
- Generating distinct subsequences of a given string in lexicographic order
- Print a number as string of 'A' and 'B' in lexicographic order
- Find a string in lexicographic order which is in between given two strings
- Print all permutations in sorted (lexicographic) order
- Power Set in Lexicographic order
- Permutations of a given string using STL
- Counts of distinct consecutive sub-string of length two using C++ STL
- Conversion of whole String to uppercase or lowercase using STL in C++
- Remove all consecutive duplicates from a string using STL in C++
- Remove duplicates from a string using STL in C++
- Find and print duplicate words in std::vector<string> using STL functions
- Count number of unique Triangles using STL | Set 1 (Using set)
- Kruskal's Minimum Spanning Tree using STL in C++
- Dijkstra’s shortest path algorithm using set in STL
- Dijkstra's Shortest Path Algorithm using priority_queue of STL