Created
October 30, 2019 09:13
-
-
Save AntonioCS/5ede240d0c9f947f322d3fce06a36b27 to your computer and use it in GitHub Desktop.
Example of permutations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/ | |
function swap(string &$str, int $i, int $ii) : void { | |
$tmp = $str[$i]; | |
$str[$i] = $str[$ii]; | |
$str[$ii] = $tmp; | |
} | |
function permute(string $str, int $l, int $r, array &$result) : void { | |
if ($l == $r) | |
$result[] = $str; | |
else { | |
for ($i = $l; $i <= $r; $i++) { | |
swap($str, $l, $i); | |
permute($str, $l + 1, $r, $result); | |
swap($str, $l, $i); | |
} | |
} | |
} | |
$str = "ABC"; | |
$result = []; | |
$n = strlen($str) - 1; | |
permute($str, 0, $n, $result); | |
var_dump($result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment