可能重复:
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.查找排列的列表中给出的置换的指数字典顺序
这是一个面试问题。让我们有一个按字典顺序排列的列表。例如,123
,132
,213
,231
,312
,321
。给定一个排列在这个列表中找到它的索引。例如,排列213
的索引是2(如果我们从0开始)。一般而言,我们可以使用next_permutation
算法以字典顺序生成下一个排列,但它会导致O(N!)解,这显然是不可行的。有没有更好的解决方案?