Skip to main content
Source Link
Bobby Jack
  • 16.1k
  • 15
  • 70
  • 99

Damn - everyone beat me to it, but I have a nice working example :)

http://www.fiveminuteargument.com/so-727707

The key is identifying the rules, which are actually quite simple:

  • Build the string char-by-char
  • At a given point in the string
    • if brackets in string so far balance (includes empty str), add an open bracket and recurse
    • if all open brackets have been used, add a close bracket and recurse
    • otherwise, recurse twice, once for each type of bracket
  • Stop when you get to the end :-)