获取所有(下一个)子集或排列(或其他东西)
我不太确定这里使用的词,所以如果我使用了错误的术语,请原谅。
我正在尝试创建一个函数来获取给出当前字符串和允许字符的字符串的下一个排列。
例如
<pre>
<?php
$current = '';
$allowed = 'ab';
function next(&$current, &$allowed) {
// This is where I need help
}
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
,Should return
a
b
aa
ab
ba
bb
aaa
aab
aba
abb
baa
bab
bba
bbb
aaaa
...等等,
我正在尝试在 PHP 和 JavaScript 中执行此操作,因此我将不胜感激任何一种语言的帮助。
I'm not quite sure of the word to use here so excuse me if I'm using the wrong terminology.
I'm trying create a function to get the next permutation of a string giving the current string and a string of allowable characters.
For example
<pre>
<?php
$current = '';
$allowed = 'ab';
function next(&$current, &$allowed) {
// This is where I need help
}
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
echo next($current, $allowed) . "\n";
Should return
a
b
aa
ab
ba
bb
aaa
aab
aba
abb
baa
bab
bba
bbb
aaaa
...and so on
I'm trying to do this in both PHP and JavaScript so I would be grateful for help in either language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)