php输出某字符串的所有不重复组合

private $result = [];
private function combination($str,$tmp){

    $long = strlen($str);
    if ($long == 0) $this->result[] = $tmp;
    echo $long;
    for ($i = 0; $i < $long; $i++)
    {
       // 首次循环已经将 tem=a,b,c,d 开头代了进去
       $this->combination(str_replace($str[$i], "", $str), $tmp . $str[$i]);

    }

}

public function show(){
    $str = 'abcd';
    $this->combination("abcd","");
   print_r($this->result);
}

另一种算法,上面的算法当有重复字符串的时候得不到想要的效果

 

private function combination2($str,$tmp){

    $long = strlen($str);
    if ($long == 0) $this->result[] = $tmp;
    for ($i = 0; $i < $long; $i++)
    {
        $a=$str[$i];
        $arr = str_split($str);
        unset($arr[$i]);
        $str2 = implode("",$arr);
        unset($arr);
        $this->combination2($str2,$tmp.$a);
        unset($str2);
    }

}

数组自行去重

执行的思路

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值