Combinations of a String

Implement a function that prints all possible combinations of a string.

A string “12” is the same as string “21”

function combine($instr, $outstr, $index)
{
    for ($i = $index; $i < strlen($instr); $i++)
    {

        $outstr = $outstr . $instr[$i];
        echo "$outstr\n";
        combine($instr, $outstr, $i + 1);
        $outstr = substr($outstr, 0, -1);
    }
}

combine("wxyz", "", 0);

This entry was posted in Algorithms. Bookmark the permalink.