Given a string str with unique characters and a number N, the task is to find the N-th lexicographic permutation of the string using Factoradic method.
Input: str = “abc”, N = 3
All possible permutations in sorted order: abc, acb, bac, bca, cab, cba
3rd permutation is bac
Input: str = “aba”, N = 2
All possible permutations in sorted order: aab, aba, baa
2nd permutation is aba
Approach: The idea is to use the concept of factoradic representation. The main concept of factoradic method is to calculate the sequence of a number. The following are the steps to find the N-th lexicographic permutation using factoradic method:
- Decrement N by 1 because this method considers sorted order as the 0th permutation.
- Divide N with 1 to the length of the string and each time store the remainder in a stack while updating the value of N as N/i.
- The calculated remainder in every step is the factoradic number. So, after calculating the final factoradic representation, start appending the element in the result string which is present on the position.
- Remove the element from the stack on each iteration.
- Repeat the above three steps until the stack becomes empty.
Lets understand this method with an example. Let the string str be “abcde” and N be 11. Then:
- Initially, 1 is subtracted from N.
N = 11 - 1 N = 10
- Now, at every iteration, divide N with i where i ranges from 1 to the length and store the remainder in a stack:
divide Remainder Quotient Factoradic 10%1 0 10 0 10%2 0 5 00 5%3 2 1 200 2%4 1 0 1200 2%5 0 0 01200
- Now, append the elements into the resultant string from the stack and continuously remove the elements from the stack. Here, the Factoradic representation of the given number is 01200. Therefore:
=a <- Selected =b =d =e =f result = a =b =c <- Selected =d =e result= ac =b =d =e <-Selected result= ace =b <- Selected =d result= aceb =d <-selected result =acebd
- Therefore, the 11th permutation of the string is “acebd”.
Below is the implementation of the above approach:
Note: An approach to find the Nth permutation with the repeating characters is discussed in this article.
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 smallest permutation of a String containing the second String as a Substring
- Lexicographic rank of a string using STL
- Find a string in lexicographic order which is in between given two strings
- Lexicographic rank of a string
- Generating distinct subsequences of a given string in lexicographic order
- Lexicographic rank of a string with duplicate characters
- Print a number as string of 'A' and 'B' in lexicographic order
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Print all permutations in sorted (lexicographic) order
- Power Set in Lexicographic order
- Largest lexicographic triplet from a given Array that forms a triangle
- Minimum length of string having all permutation of given string.
- Check if any permutation of string is a K times repeated string
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Print all permutation of a string using ArrayList
- Find n-th lexicographically permutation of a string | Set 2
- Find the Nth occurrence of a character in the given String
- Find nth Fibonacci number using Golden ratio
- Find Nth even length palindromic number formed using digits X and Y
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.