在给定字符串的排列的排序列表中查找给定排列的索引
我们得到一个字符串和该字符串的排列。
例如,输入字符串 sandeep
和排列 psdenae
。
查找给定排列在原始字符串排列的排序列表中的位置。
We're given a string and a permutation of the string.
For example, an input string sandeep
and a permutation psdenae
.
Find the position of the given permutation in the sorted list of the permutations of the original string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
给定长度为 n 的字符串的排列总数将为 n! (如果所有字符都不同),因此不可能探索所有组合。
这道题其实就像数学P& C 题
求单词“stack”按字典顺序排列时的排名。
给定输入字符串 NILSU
取一个单词,我们要找到它的排名。以“SUNIL”为例。
因此,如果我们计算使用 SUNIL 字母按字典顺序排列可以创建的单词,则 SUNIL 一词将位于第 95 位。
因此通过这个方法你可以很轻松的解决这个问题。
The total number of permutation of a given string of length n would be n! (if all characters are different), thus it would not be possible to explore all the combinations.
This question is actually like the mathematics P & C question
Find the rank of the word "stack" when arranged in dictionary order.
Given the input string as NILSU
Take a word which we have to find the rank. Take "SUNIL" for example.
So the word SUNIL will be at 95th position if we count the words that can be created using the letters of SUNIL arranged in dictionary order.
Thus through this method you could solve this problem quite easily.
构建@Algorithmist的答案以及他对答案的评论,并使用这篇文章中讨论的原则是重复的字母,我在 JavaScript 中编写了以下算法,该算法适用于所有基于字母的单词,即使有重复的字母实例也是如此。
我没有评论代码,但我确实尝试使变量名称尽可能具有解释性。如果您使用开发工具控制台通过调试器进程运行它,并放入一些 console.logs,您应该能够看到它如何使用上面链接的 SO 帖子中的公式。
Building off @Algorithmist 's answer, and his comment to his answer, and using the principle discussed in this post for when there are repeated letters, I made the following algorithm in JavaScript that works for all letter-based words even with repeated letter instances.
I didn't comment the code but I did try to make the variable names as explanatory as possible. If you run it through a debugger process using your dev tools console and throw in a few console.logs you should be able to see how it uses the formula in the above-linked S.O. post.
我尝试用js实现这个。它适用于没有重复字母的字符串,但否则我会得到错误的计数。这是我的代码:
I tried to implement this in js. It works for string that have no repeated letters but I get a wrong count otherwise. Here is my code:
有点晚了,但仅供参考...您可以直接使用此 C# 代码。
它会起作用,但是......
唯一重要的是,通常,您应该以独特的值作为起始集。否则你就没有n!排列。你还有别的东西(少于n!)。当项目可能是重复的时,我有点怀疑是否有任何有用的用途。
A bit too late but just as reference... You can use this C# code directly.
It will work but...
The only important thing is that usually, you should have unique values as your starting set. Otherwise you don't have n! permutations. You have something else (less than n!). I have a little doubt of any useful usage when item could be duplicate ones.
我解决这个问题的方法是对给定的排列进行排序。
字符串中字符的交换次数将为我们提供排列在排列排序列表中的位置。
My approach to the problem is sort the given permutation.
Number of swappings of the characters in the string will give us the position of the pemutation in the sorted list of permutations.
一个低效的解决方案是连续查找先前的排列,直到到达无法再排列的字符串。达到此状态所需的排列数就是原始字符串的位置。
但是,如果您使用组合数学,您可以更快地获得解决方案。如果字符串长度超过 12,前面的解决方案将产生非常慢的输出。
An inefficient solution would be to successively find the previous permutations until you reach a string that cannot be permuted anymore. The number of permutations it takes to reach this state is the position of the original string.
However, if you use combinatorics you can achieve the solution faster. The previous solution will produce a very slow output if string length exceeds 12.