php输出某字符串的所有不重复组合
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);
}
}
数组自行去重
执行的思路
php输出某字符串的所有不重复组合相关教程