3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Returns the total number of $count-length strings generatable from $letters. function getPermCount($letters, $count) { $result = 1; // k characters from a set of n has n!/(n-k)! possible combinations for($i = strlen($letters) - $count + 1; $i <= strlen($letters); $i++) { $result *= $i; } print_r("PermCount: " . $result); return $result; } // Decodes $index to a $count-length string from $letters, no repeat chars. function getPerm($letters, $count, $index) { $result = array(); for($i = 0; $i < $count; $i++) { $pos = $index % strlen($letters); $result[] = $letters[$pos]; $index = ($index-$pos)/strlen($letters); $letters = substr($letters, 0, $pos) . substr($letters, $pos+1); } sort($result); $result = array_unique($result, SORT_REGULAR); return implode("", $result); } $r = array(); //$letters = 'abcdefghijklm'; $letters = 'abcdefg'; $len = strlen($letters); for ($c = 1; $c <= $len; $c++) { for($i = 0; $i < getPermCount($letters, $c); $i++) $r[] = getPerm($letters, $c, $i); } //print_r(array_unique($r, SORT_REGULAR)); $r = array_unique($r, SORT_REGULAR); print_r(count($r));

preferences:
32.31 ms | 402 KiB | 5 Q