Last active
May 9, 2016 06:52
-
-
Save niksmac/07e8c3cfddb2ce7e85d7aeca9123031d to your computer and use it in GitHub Desktop.
Prints all combinations of n-pairs of balanced parentheses using PHP
This file contains hidden or 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 | |
| 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); | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
prints out