增量创建字符串的所有排列 C#
我正在尝试创建一个函数,该函数将以增量方式创建字符串的所有排列。我想首先:
AAAAA
...
AAAAB
...
ACCCC
...
...
ZZZZZ
我环顾四周,似乎找不到任何类似的东西。我尝试创建它,但它不是增量的。
有什么建议吗?
I am trying to create a function that will create all permutations of a string in an incremental fashion. I would like to start at:
AAAAA
...
AAAAB
...
ACCCC
...
...
ZZZZZ
I have looked around, and can't seem to find anything of that sort. I tried to create it, but it wasn't incrementally.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您所描述的“排列”更广为人知的是笛卡尔积。如果您需要形成笛卡尔积的任意数量的序列,请参阅我对此主题的回答:
生成所有可能的组合
The "permutation" you are describing is better known as the Cartesian product. If you have an arbitrary number of sequences that you need to form the Cartesian product of, see my answer to this question on the subject:
Generating all Possible Combinations
通常我不会帮助这些蛮力类型的结果......但是看到你会从集合中得到多少无用的结果,我想我只是把它扔进去。
顺便说一句......如果你只想要下一个值,你可以做这样的事情...
Normally I wouldn't help these brute force type results... but seeing how many useless result you will get out of the set I figured I'd just toss this in.
BTW... if you just want the next value you can do something like this...
一个不同的变体,我想到了使用模运算。请注意,我将字符降低到 {A,B,C} 来测试它,因为将 5 个字母提高到 Z 会产生很多字符串。
A different variant where I had the idea of using modulo arithmetic. Note that I lowered the character to {A,B,C} to test it, since going up to Z for 5 letters is a lot of strings.
很快就被打败了——我希望这可以做得更好:
Bashed out quickly - I expect this could be done better:
这是 LINQPad 友好的代码,它使用 lambda 表达式。
Here is the LINQPad friendly code and it uses lambda expression.