Skip to content

Instantly share code, notes, and snippets.

@niksmac
Last active May 9, 2016 06:52
Show Gist options
  • Select an option

  • Save niksmac/07e8c3cfddb2ce7e85d7aeca9123031d to your computer and use it in GitHub Desktop.

Select an option

Save niksmac/07e8c3cfddb2ce7e85d7aeca9123031d to your computer and use it in GitHub Desktop.
Prints all combinations of n-pairs of balanced parentheses using PHP
<?php
function printbraces($n) {
if($n>0) {
start_printing("", $n, $n);
}
}
function start_printing($s, $o, $c) {
if($o > $c)
return;
if($o == 0 && $c == 0){
echo $s . "<br/>";
return;
}
if($o < 0 || $c < 0)
return;
start_printing($s . '{',$o-1,$c);
start_printing($s . '}',$o,$c-1);
}
printbraces(2);
?>
@niksmac
Copy link
Copy Markdown
Author

niksmac commented May 9, 2016

prints out

{{}}
{}{}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment