获取所有(下一个)子集或排列(或其他东西)

发布于 2024-10-17 11:32:50 字数 1011 浏览 1 评论 0原文

我不太确定这里使用的词,所以如果我使用了错误的术语,请原谅。

我正在尝试创建一个函数来获取给出当前字符串和允许字符的字符串的下一个排列

例如

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终弃我 2024-10-24 11:32:50
function nextPermutation(&$current, $allowed) {
    if (empty($current)) {
        $current = $allowed[0];
    } else {
        for ($i = strlen($current) - 1; $i >= 0; $i--) {
            $index = strpos($allowed, $current[$i]);
            if ($index < strlen($allowed) - 1) {
                $current[$i] = $allowed[$index + 1];
                break;
            } else {
                $current[$i] = $allowed[0];
                if ($i == 0) {
                    $current = $allowed[0] . $current;
                    break;
                }
            }
        }
    }
    return $current;
}
function nextPermutation(&$current, $allowed) {
    if (empty($current)) {
        $current = $allowed[0];
    } else {
        for ($i = strlen($current) - 1; $i >= 0; $i--) {
            $index = strpos($allowed, $current[$i]);
            if ($index < strlen($allowed) - 1) {
                $current[$i] = $allowed[$index + 1];
                break;
            } else {
                $current[$i] = $allowed[0];
                if ($i == 0) {
                    $current = $allowed[0] . $current;
                    break;
                }
            }
        }
    }
    return $current;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文