The permutations of the string,A permutation of a string is a new string that is created by rearranging the characters of the original string.
You’re absolutely right — a permutation of a string is a rearrangement of its characters. For example, for the string “abcd”, its permutations are:
OUTPUT :-
a,b,c,d
b,a,c,d
b,c,a,d
b,c,d,a
a,c,b,d
c,a,b,d
c,b,a,d
c,b,d,a
a,c,d,b
c,a,d,b
c,d,a,b
c,d,b,a
a,b,d,c
b,a,d,c
b,d,a,c
b,d,c,a
a,d,b,c
d,a,b,c
d,b,a,c
d,b,c,a
a,d,c,b
d,a,c,b
d,c,a,b
d,c,b,a
Here’s a PHP function to generate all permutations of a given string:
$s = “abcd”;
$list = str_split($s,1);;
foreach (findAllPermutations($list) as $permutation) {
echo implode(‘,’, $permutation) . “
“;
}
function findAllPermutations(array $elements)
{
if (count($elements) <= 1) { yield $elements; } else { foreach (findAllPermutations(array_slice($elements, 1)) as $permutation) { foreach (range(0, count($elements) - 1) as $i) { yield array_merge( array_slice($permutation, 0, $i), [$elements[0]], array_slice($permutation, $i) ); } } } }