Skip to content

Instantly share code, notes, and snippets.

@AntonioCS
Created October 30, 2019 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntonioCS/5ede240d0c9f947f322d3fce06a36b27 to your computer and use it in GitHub Desktop.
Save AntonioCS/5ede240d0c9f947f322d3fce06a36b27 to your computer and use it in GitHub Desktop.
Example of permutations
<?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