Given a string s that may have duplicate characters. Find out the lexicographic rank of s. s may consist of lower as well as upper case letters. We consider the lexicographic order of characters as their order of ASCII value. Hence the lexicographical order of characters will be ‘A’, ‘B’, ‘C’, …, ‘Y’, ‘Z’, ‘a’, ‘b’, ‘c’, …, ‘y’, ‘z’.
Input : “abab”
Output : 21
Explanation: The lexicographical order is: “aabb”, “abab”, “abba”, “baab”, “baba”, “bbaa”. Hence the rank of “abab” is 2.
Input : “settLe”
Output : 107
Prerequisite: Lexicographic rank of a string
Method: The method here is a little different from the without repetition version. Here we have to take care of the duplicate characters also. Let’s look at the string “settLe”. It has repetition(2 ‘e’ and 2 ‘t’) as well as upper case letter(‘L’). Total 6 characters and the total number of permutations are 6!/(2!*2!).
Now there are 3 characters(2 ‘e’ and 1 ‘L’) on the right side of ‘s’ which come before ‘s’ lexicographically. If there were no repetition then there would be 3*5! smaller strings which have the first character less than ‘s’. But starting from position 0, till the end there are 2 ‘s’ and 2 ‘t'(i.e. repetitions). Hence, the number of possible smaller permutations with the first letter smaller than ‘s’ are (3*5!)/(2!*2!).
Similarly, if we fix ‘s’ and look at the letters from index 1 to end then there is 1 character(‘L’) lexicographically less than ‘e’. And starting from position 1 there are 2 repeated characters(2 ‘e’ and 2 ‘t’). Hence, the number of possible smaller permutations with first letter ‘s’ and second letter smaller than ‘e’ are (1*4!)/(2!*2!).
Similarly, we can form the following table:
1. Initialize t_count(total count) variable to 1(as rank starts from 1). 2. Run a loop for every character of the string, string[i]: (i) using a loop count less_than(number of smaller characters on the right side of string[i]). (ii) take one array d_count of size 52 and using a loop count the frequency of characters starting from string[i]. (iii) compute the product, d_fac(the product of factorials of each element of d_count). (iv) compute (less_than*fac(n-i-1))/(d_fac). Add it to t_count. 3. return t_count
Rank of abab is: 2 Rank of settLe is: 107
This algorithm runs in
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.
- Lexicographic rank of a string
- Lexicographic rank of a string using STL
- Lexicographic rank of a string among all its substrings
- Lexicographic smallest permutation of a String containing the second String as a Substring
- Generating distinct subsequences of a given string in lexicographic order
- Print a number as string of 'A' and 'B' in lexicographic order
- Find the N-th lexicographic permutation of string using Factoradic method
- Find a string in lexicographic order which is in between given two strings
- Count the nodes of a tree whose weighted string does not contain any duplicate characters
- Java program to print all duplicate characters in a string
- Remove all duplicate adjacent characters from a string using Stack
- Print all permutations in sorted (lexicographic) order
- Power Set in Lexicographic order
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Subsequences generated by including characters or ASCII value of characters of given string
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.