用点生成所有可能性

时间:2016-04-24 19:56:15

标签: php algorithm

我正在寻找PHP中的算法,用dot输出所有可能性。产生我们可以放在任何字的地方,但现在允许彼此重复两个点。例如Shaghayegh 输出如下:

S.haghayegh
Sh.aghayegh
Shag.hayegh
Sh.agha.ye.gh

不允许的输出:

s..haghayegh (repeat dots right after each other)
.shaghayegh (put dots at first of word)
shaghayegh. (put dots at end of word)

1 个答案:

答案 0 :(得分:0)

参考我的this answer,我已经解决了:

<?php
    header("Content-type: text/plain");
    $var = "Shaghayegh";
    for ($i = 1; $i < strlen($var); $i++) {
        $avar = str_split($var);
        array_splice($avar, $i, 0, array("."));
        echo "$i: " . implode("", $avar) . "\n";
    }
?>

输出将是:

1: S.haghayegh
2: Sh.aghayegh
3: Sha.ghayegh
4: Shag.hayegh
5: Shagh.ayegh
6: Shagha.yegh
7: Shaghay.egh
8: Shaghaye.gh
9: Shaghayeg.h

如果我将其更改为Praveen,则会显示:

1: P.raveen
2: Pr.aveen
3: Pra.veen
4: Prav.een
5: Prave.en
6: Pravee.n