Possible Duplicate:
Given a string and permutation of the string. Find the index of this permuted string in the sorted list of the permutations of the string.
This is an interview question. Let there is a list of permutations in lexicographic order. For example, 123
, 132
, 213
, 231
, 312
, 321
. Given a permutation find its index in such a list. For example, the index of permutation 213
is 2 (if we start from 0).
Trivially, we can use a next_permutation
algorithm to generate a next permutation in lexicographic order, but it leads to O(N!) solution, which is obviously non-feasible. Is there any better solution?