Removing Duplicates in Permutation of an Input String using PHP

Viewed 24

I cannot remove the duplicate results even after using array_unique and all.

<?php 

function permute($str, $l, $r) 
{
    if ($l == $r) 
    {

        $uniqueStr = implode(',', array_unique(explode(',', $str))); 
        echo $uniqueStr . "\n"; 
    }
    else
    { 
        for ($i = $l; $i <= $r; $i++) 
        { 
            $str = swap($str, $l, $i); 
            permute($str, $l + 1, $r); 
            $str = swap($str, $l, $i); 
        } 
    } 
} 

function swap($a, $i, $j) 
{ 
    $temp; 
    $charArray = str_split($a); 
    $temp = $charArray[$i] ; 
    $charArray[$i] = $charArray[$j]; 
    $charArray[$j] = $temp; 
    return implode($charArray); 
} 
  
$str = "aabb"; 
$n = strlen($str); 
permute($str, 0, $n - 1); 
?>

Expected Output:

enter image description here

Current output:

enter image description here

0 Answers
Related