Combinations of a String

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

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

Note : The below function will work for unique strings like “wxyz” but not for “aabc”.  One quick to fix it for “aabc” would be to store the output strings in a hash and then output the hash.

Check this video for good explanation of “AABC” :

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);