class Mutation
{
private $len = 0;
private $arr = [];
private $table = [];
private $result = [];
public function __construct(array $arr, int $len)
{
$this->len = $len;
$this->setHashTable($arr);
$this->arr = array_unique($arr);
}
private function setHashTable(array $arr)
{
foreach ($arr as $value) {
isset($this->table[$value]) ? $this->table[$value] += 1 : $this->table[$value] = 1;
}
}
private function build()
{
$this->result = array_reduce(array_fill(0, $this->len - 1, $this->arr), function ($previous, $current) {
$result = [];
foreach ($current as $a) {
foreach ($previous as $b) {
if (is_array($b)) {
$values = array_count_values($b);
if (empty($values[$a]) || $this->table[$a] > $values[$a]) {
$result[] = array_merge([$a], $b);
}
} elseif ($a !== $b || $this->table[$a] > 1) {
$result[] = array_merge([$a], [$b]);
}
}
}
return $result;
}, $this->arr);
}
public function print()
{
empty($this->result) && $this->build();
print_r($this->result);
}
public function getCount()
{
empty($this->result) && $this->build();
return count($this->result);
}
}
$mutation = new Mutation([1, 2, 3], 2);
$mutation->print();
echo $mutation->getCount();